Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question
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. –  Stephane Chazelas Mar 9 '13 at 0:48
    
@StephaneChazelas You are correct. –  jordanm Mar 9 '13 at 0:52
add comment

1 Answer

up vote 3 down vote accepted

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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