从一个文件中读取字符串,grep另一个文件中的第一个匹配项 [英] Read string from one file, grep the first occurrence in another file
问题描述
我正在从文件devices_list.txt中读取一个字符串.
I'm reading a string from file, appliances_list.txt.
appliances_list.txt包含
appliances_list.txt contains
fridge
dryer
ironbox
microwave
我正在读取的文件是myappliances.txt.内容是
The file I'm reading is myappliances.txt. Content is
I have a fridge
I have another fridge
I have a refridgerator
I have a microwave
I have ironbox at home
I have another microwave
I have a hairdryer
我正在使用
grep -o -m1 -f appliances_list.txt myappliances.txt
输出为
fridge
我想要的输出是每个字符串的第一次出现(完全匹配)
My desired output is, first occurrence of each string (exact match)
fridge
microwave
ironbox
有人可以指出我正确的方向吗?
Can someone point me in the right direction?
推荐答案
$ cat tst.awk
NR==FNR { strings[$0]; ++numStrings; next }
{
for (i=1;i<=NF;i++) {
if ($i in strings) {
print $i
delete strings[$i]
if (--numStrings == 0) {
exit
}
break
}
}
}
$ awk -f tst.awk appliances_list.txt myappliances.txt
fridge
microwave
ironbox
这将非常有效,因为它会从找到的字符串列表中删除每个找到的字符串,因此每一行的比较需要的次数更少,并且当列表中没有更多的字符串时将退出程序,因此不会浪费程序时间读取第二个文件的其余行.
That will be very efficient since it will remove each found string from the list of strings as it's found so there's fewer comparisons necessary on every line and when there's no more strings in the list will exit the program so it doesn't waste time reading the remaining lines of the 2nd file.
这篇关于从一个文件中读取字符串,grep另一个文件中的第一个匹配项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!