我想提取一行,如果它包含文本文件的指定列中的單詞。 我該如何在單線unix命令上做到這一點?也許與cat
,echo
,cut
,grep
與幾個piples或東西。提取一行,如果它包含指定列中的單詞
我有這種格式
#SentenceID<tab>Sentence1<tab>Sentence2<tab>Other_unknown_number_of_columns<tab> ...
文本文件的一個例子看上去文本文件看起來是這樣的:
021348 this is the english sentence with coach . c'est la phrase française avec l'entraîneur . And then there are several nonsense columns like these .
923458 this is a another english sentence without the word . c'est une phrase d'une autre anglais sans le bus mot . whatever foo bar nonsense columns 2134234 $%^&
命令應輸出,如果我要找的字是coach
在第二列:
021348 this is the english sentence with coach . c'est la phrase française avec l'entraîneur . And then there are several nonsense columns like these .
我可以用python做到這一點,但我正在尋找一個unix命令或一行代碼:
outfile = open('out.txt')
for line in open('in.txt'):
if "coach" in line.split():
print>>outfile, line
+1但注意awks默認塊是'{print}'所以你需要的只是'awk -F'\ t''$ 2〜/ coach /'文件' – 2013-03-26 10:50:58
很高興認識到,@sudo_O!我用你的建議更新我的答案。謝謝! – fedorqui 2013-03-26 10:54:59