文件1
pears
oranges
apples
pear
pears
pears
apples
apples
grapes
grapes
straw
文件2
apples
pear
grapes
straw
我需要比较文件1和2并获得匹配项。如果重复,则也应包括在内。
我尝试过的
grep -f file 1 file 2
哪个给我
apples
pear
grapes
straw
但是我想要的是也包括重复映射到文件2的
apples
apples
apples
pear
grapes
grapes
straw
答案 0 :(得分:2)
请您尝试以下。
awk 'FNR==NR{a[$0]=(a[$0]?a[$0] ORS:"")$0;next} ($0 in a){print a[$0]}' Input_file1 Input_file2
说明: 添加上述代码的说明。
awk ' ##Starting awk program here.
FNR==NR{ ##Checking condition FNR==NR which will be TRUE when Input_file1 is being read.
a[$0]=(a[$0]?a[$0] ORS:"")$0 ##Creating an array named a whose index is $0 and its keep concatenating its own values with new line and current line value.
next ##next will skip all further statements from here.
} ##Closing BLOCK for FNR==NR condition here.
($0 in a){ ##Checking condition if $0 is present in array a then do following.
print a[$0] ##Printing array a value whose index is $0.
}
' Input_file1 Input_file2 ##Mentioning Input_file names here.
输出如下。
apples
apples
apples
pear
grapes
grapes
straw
答案 1 :(得分:2)
您要匹配固定字符串,并且要匹配整行,因此请将-F
和-x
选项添加到grep。
您需要做的就是将file2设置为病毒码文件:
grep -Fxf file2 file1
答案 2 :(得分:2)
如果您以grep更改文件顺序,它将为您提供所需的记录,但以原始顺序,还添加了正确的选项。
$ grep -xFf file2 file1
apples
pear
apples
apples
grapes
grapes
straw
但是,如果您希望按文件2的顺序使用它们,awk
是更好的解决方案。
这是没有awk
$ join -2 2 <(sort file1) <(nl file2 | sort -k2) | sort -k2n | cut -d' ' -f1
apples
apples
apples
pear
grapes
grapes
straw