我有 2 个文件,它们都有 2 列。如果 2.txt 中有相同的列,我希望从 1.txt 中删除行。
例子 -
1.txt(包含)
example:test
example2:test2
example3:test3
2.txt包含(包含)
example:example
example2:example
example3:example
example4:example
example5:example
因此,在 1.txt 第 1 列中,条目与 2.txt 相同,因此这种情况下的预期输出将是
example4:example
example5:example
删除的内容 -
example:test
example2:test2
example3:test3
答案1
问题描述可能更清楚,但根据示例,我认为您想要这样:
awk -F: 'NR==FNR { d[$1]=1; next } !($1 in d) { print $0 }' 1.txt 2.txt
该-F:
参数表示“将冒号字符 ':' 视为列分隔符”。 (默认情况下,awk
将连续的空格视为列分隔符。)
第二个参数是一个awk
程序,它说
IF this line is from the first input file THEN
In a dictionary named `d`, create an item whose key is the first column of the input line and whose value is 1
Skip the rest of the program move on to process the next line
(because of the "skip to next line" above we only do this for lines from the second file)
IF the dictionary named `d` has no item whose whose key is the first column of this line THEN
Print this line
答案2
使用下面的命令效果很好
awk 'NR==FNR {a[$0];next}($0 in a) {print $0}' 1.txt 2.txt