2013-03-26 54 views
2

我想提取一行,如果它包含文本文件的指定列中的單詞。 我該如何在單線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 

回答

4

這是怎麼回事?

awk -F'\t' '{if($2 ~ "coach") print} your_file 
  • -F'\t' - >使得分隔符是標籤。
  • $2 ~ "coach" - >在第二個字段中尋找「coach」。
  • print $0print - >打印整行。

編輯

sudo_O已經提出了以下,其中更短:

awk -F'\t' '$2~/coach/' file 
+1

+1但注意awks默認塊是'{print}'所以你需要的只是'awk -F'\ t''$ 2〜/ coach /'文件' – 2013-03-26 10:50:58

+1

很高興認識到,@sudo_O!我用你的建議更新我的答案。謝謝! – fedorqui 2013-03-26 10:54:59

1

對於這種需求,我總是用AWK:

awk的-F '\ t''$ 2〜/ coach/{print $ 0;}'< textFile

您可以使用$ x訪問所有列,$ 0包含整個行。這個測試是用regexp進行的,在這種情況下非常簡單,所以如果你的需求變得更復雜,這真的很有用。

相關問題