gpt4 book ai didi

unix - 如果某行包含指定列中的单词,则提取该行

转载 作者:行者123 更新时间:2023-12-02 01:12:41 24 4
gpt4 key购买 nike

如果某行包含文本文件指定列中的单词,我想提取该行。 我怎样才能在单行 unix 命令上做到这一点? 也许可以使用 catechocut, grep 有几个小疙瘩什么的。

我有一个看起来是这种格式的文本文件

#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

最佳答案

这个呢?

awk -F'\t' '{if($2 ~ "coach") print} your_file
  • -F'\t' --> 使分隔符成为制表符。
  • $2 ~ "coach" --> 在第二个字段中查找“coach”。
  • print $0print --> 打印整行。

编辑

sudo_O建议如下,甚至更短:

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

关于unix - 如果某行包含指定列中的单词,则提取该行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15633512/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com