我正在从文件appliance_list.txt中读取一个字符串。
appliances_list.txt包含
fridge
dryer
ironbox
microwave
我正在阅读的文件是myappliances.txt。内容是
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
我想要的输出是,第一次出现每个字符串(完全匹配)
fridge
microwave
ironbox
有人能指出我正确的方向吗?
答案 0 :(得分:1)
一个使用awk:
$ awk 'NR==FNR{a[$1]=1;next}a[$2]==1{print $2;a[$2]++}' pattern sample
Pattern1
Pattern2
Pattern3
说明:
$ awk ' # well, awk
NR==FNR { # for the first file
a[$1]=1 # hash each keyword and set it as 1
next # next record
}
a[$2]==1 { # second file, if the word in 2nd column was hashed and 1
print $2 # output it
a[$2]++ # and increase its value above 1
}' pattern sample # mind the order