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?

  • 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 Mar 8 '13 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 Mar 8 '13 at 23:11
  • 2
    @jordanm, no need for + or * here. Also, looks like awk '$3 >= 20 {print $2} may be even more appropriate here. – Stéphane Chazelas Mar 9 '13 at 0:48
  • @StephaneChazelas You are correct. – jordanm Mar 9 '13 at 0:52
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.