使用grep搜索

时间:2011-06-16 21:15:09

标签: grep

您如何使用grep搜索列表中的特定字段?例如,在myfile.txt文件中,我需要搜索单词apple。谢谢,

banana   nut      mango    raisin
plum     peach    cherry   orange
pear     grape    pomelo   apple
papaya   kiwi     avocado  strawberry

2 个答案:

答案 0 :(得分:0)

这取决于你想要的输出。

'grep -w apple myfile.txt'将是一个好的开始

导致'梨葡萄柚苹果'

如果您只想在第4列搜索特定单词,可以先使用awk仅打印第4列,然后grep结果

awk'{print $ 4}'< myfile.txt | grep -w apple 这导致了'苹果'

答案 1 :(得分:0)

cat myfile.txt | tr -s ' ' | cut -d' ' -f4 | grep 'apple'