我希望比较Linux上2个.csv文件的值,当第一个文件的第一列(始终是IP)与第二个文件中的任何IP匹配时,排除第一个文件中的行。
通过命令行执行此操作的任何方式对我都有好处(例如通过grep),我可以。
File1.csv是:
10.177.33.157,IP,Element1
10.177.33.158,IP,Element2
10.175.34.129,IP,Element3
10.175.34.130,IP,Element4
10.175.34.131,IP,Element5
File2.csv:
10.177.33.157 < Exists on the first file
10.177.33.158 < Exists on the first file
10.175.34.129 < Exists on the first file
80.10.2.42 < Does not exist on the first file
80.10.3.194 < Does not exist on the first file
所需的输出文件:
10.175.34.130,IP,Element4
10.175.34.131,IP,Element5
答案 0 :(得分:1)
只需 awk
:
awk -F',' 'NR==FNR{ a[$1]; next }!($1 in a)' file2.csv file1.csv
输出:
10.175.34.130,IP,Element4
10.175.34.131,IP,Element5
答案 1 :(得分:1)
使用-f
中的grep
选项来比较文件。 -v
反转匹配。 -F
用于固定字符串。 man grep
有很长的路要走。
-f FILE, --file=FILE
Obtain patterns from FILE, one per line. The empty file contains
zero patterns, and therefore matches nothing. (-f is specified by POSIX.)
-v, --invert-match
Invert the sense of matching, to select non-matching lines. (-v is specified by POSIX.)
-F, --fixed-strings, --fixed-regexp
Interpret PATTERN as a list of fixed strings, separated by newlines, any of which is to be matched. (-F is specified by POSIX,
--fixed-regexp is an obsoleted alias, please do not use it new scripts.)
结果:
$ grep -vFf f2.csv f1.csv
10.175.34.130,IP,Element4
10.175.34.131,IP,Element5