2

I have this command

$ cut -f2,3 AIS2F1 | grep [2-9][0-9]*  | cut -f1

So my second and third fields are something like this

Ben 434
Me  12
you 56

So, I thought that the logic should be to cut the second and third field then grep numbers that are bigger than 20 and then cut the first field. That should give me the name that is on the same line with number that is bigger than 20, but it gives me this output:

Ben 
Me
you 

instead of

Ben
you 

which is what I want, how can I fix this command?

4
  • 1
    You need to quote your arguments to grep. That regex is also a valid shell glob, and could be expanded by the shell before grep sees it
    – jordanm
    Commented Mar 8, 2013 at 23:09
  • 1
    Not an answer, but awk is better suited here as well: awk '$3 ~ /[2-9][0-9]+/ { print $2 }' AIS2F1
    – jordanm
    Commented Mar 8, 2013 at 23:11
  • 2
    @jordanm, no need for + or * here. Also, looks like awk '$3 >= 20 {print $2} may be even more appropriate here. Commented Mar 9, 2013 at 0:48
  • @StephaneChazelas You are correct.
    – jordanm
    Commented Mar 9, 2013 at 0:52

1 Answer 1

3

The * in grep means that zero or one of the previous occurrences will be matches. Thus, your grep command matches every line containing a [2-9]. Replace the * with a \+, which means: match one or more occurrence.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .