[英]How to compare two files to create a third file with the all possible pair of values correspond to one row of first file?
[英]How to compare entries in one file to two files?
我有一个文件(名为 file1),其中包含名称及其 IP。 它看起来像这样:-
VM1 2.33.4.22
VM2 88.43.21.34
VM3 120.3.45.66
VM4 99.100.34.5
VM5 111.3.4.66
我有两个文件(file2 和 file3),它们仅由 IP 组成。
文件 2 包括:-
120.3.45.66
88.43.21.34
文件 3 包括:-
99.100.34.5
我想将 file1 与 file2 和 file3 进行比较,并获取 file2 和 file3 中不存在的名称和 IP。 所以 output 将是:
VM1 2.33.4.22
VM5 111.3.4.66
我怎样才能得到想要的 output?
sed 's/\./\\./g; s/.*/ &$/' file2 file3 | grep -vf - file1
-f -
从标准输入获取模式列表,使用-v
打印文件 1 中的不匹配行。您可以编写一个 shell 脚本来为您完成。
#!/bin/sh
cat $1.txt $2.txt > mergedFile.txt
grep -v -f mergedFile.txt $3.txt
您可以使用以下命令运行脚本sh check.sh file2 file3 file1
awk 'NR==FNR { out[$1]=1; next} !out[$2]' <(/bin/cat file2 file3) file1
这使用与sed
解决方案基本相同的东西,使用awk
代替。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.