searching using grep
hi how do you search for a specific field in a lis开发者_运维问答t using grep? For example in a file myfile.txt I need to search for the word apple. Thanks,
banana nut mango raisin
plum peach cherry orange
pear grape pomelo apple
papaya kiwi avocado strawberry
it depends what exactly you want as output.
'grep -w apple myfile.txt' would be a good start
which results in 'pear grape pomelo apple'
If you just want to search the 4th column for a specific word, you could first use awk to print only the 4th column, and then grep the result
awk '{print $4}' < myfile.txt | grep -w apple which results in just 'apple'
cat myfile.txt | tr -s ' ' | cut -d' ' -f4 | grep 'apple'
精彩评论