ubuntu - AWK 提取列中具有相同单词的前两行

标签 ubuntu awk grep tabs

我需要从一个大的多列文件(500mb 到 1gb,\t 分隔符)中提取前两行,其中包含出现在特定列中的 100.000 多个单词的列表。
现在我正在使用这样的循环:

while read GREP
do
grep -m 2 "${GREP}" input.txt > output.txt ;
done < list_of_words.txt
但这需要太多时间(我需要对许多文件执行此操作),所以我正在寻找替代方案。
一个简单的fgrep -f -m2不起作用,因为 -m 适用于合并命中的整个输出。
我想 awk 可能是一个解决方案,但无法在线找到帮助。
例如,如果输入文件是:
Dog Bird House
Mouse Giraffe Cat
Mouse Rhino House
Lion Horse House
Dog Rat Cat
Dog Mice Cat
我想要一个这样的输出文件(行的顺序不相关):
Dog Bird House
Mouse Rhino House
Mouse Giraffe Cat
Dog Rat Cat
我现在正在使用这样的单词列表:
House
Cat
但是,如果这是一种仅保留前两行的方法,那么第三列上的相同单词会更好!
注意:第三列的单词将是唯一的,不会出现在任何其他列中!

最佳答案

像这样的东西?:

$ awk -F"\t" 'NR==FNR{a[$0]=2;next}($3 in a)&&--a[$3]>=0' list input
输出:
Dog     Bird    House
Mouse   Giraffe Cat
Mouse   Rhino   House
Dog     Rat     Cat
解释:
$ awk -F"\t" '           # yes awk yes, fields tab delimited
NR==FNR {                # process the first file, list of words
    a[$0]=2              # hash every word, set initial value to 2
    next                 # on to the next word
}                        # process second file below this point
($3 in a) && --a[$3]>=0  # if 3rd field word in a and seen max once, output
' list input             # mind the file order

关于ubuntu - AWK 提取列中具有相同单词的前两行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64121772/

相关文章:

linux - Ubuntu 12.04 和 MPICH 性能

linux - Bash - 查找最大文件并打印其路径

awk - 如何使用awk将特定分隔符的引号添加到行尾?

linux - Grep Linux 中字符串的特殊部分

linux - Selenium RC,在 Linux 上,有没有办法让所有浏览器实例在后台运行?

ubuntu - 如何从 Elementary OS 0.2 中删除 Midori?

function - awk - 测试打印是否成功

linux - bash 脚本中的 awk 脚本

linux - 将文件修改日期和 "grep"结果通过 "find"组合在一行中

regex - 解析shell脚本中的键值