我有两个文件,如下所示。file1有两列,file2有不同的列数,这取决于行。我想比较这两个文件,如果$1和$2都在file1的一行file2中,我想删除这一行。另外,file2是逗号分隔的。如何使用awk来完成这一任务?或者其他的文字处理工具?
file1
5052 5051
4952 4951 file2
2001, 5052, 7001, 5051, 1000
2002, 5052, 7001, 1500, 2500
2003, 5051, 3500, 4500, 4952
2004, 4952, 4999, 4500, 4951 预期输出:
2002, 5052, 7001, 1500, 2500
2003, 5051, 3500, 4500, 4952 我已经尝试了下面的awk代码,但没有运行。
awk 'NR==FNR{A[$1]=$1;A[$2]=$2; next} {if ($0=A[$1] && $0=A[$2]){next} else {print $0}' file1 file2 >> test.inp发布于 2021-01-15 09:09:14
awk 'NR==FNR { map[$1]="1";map1[$2]="1";next } {lin=gensub(" ","","g",$0);split(lin,map3,",");ok=1;for (i in map3) { if (map1[map3[i]]==1 || map[map3[i]]==1 ) { ok=0 } } if (ok==1) { print $0 } }' file1 file2解释:
awk 'NR==FNR { # Process the first file
map[$1]=""; # Set up two arrays, one for
the first space delimited
field and the other for
the second
map1[$2]="";
next # Skip to the next record
}
{
lin=gensub(" ","","g",$0); # Process the second file and remove
all spaces from the the line
putting the result in a variable
lin
split(lin,map3,","); # Split the variable lin into the
array map3 based on commas as the
separator
ok=1; # Initialise a variable
for (i in map3) {
if (map1[map3[i]] || map[map3[i]]) {
ok=0 # Loop through each entry in the map3
array (on the line) and check if it
exists in map1 or map. If it does
exist, set ok to 0
}
}
if (ok==1) {
print $0 # Only if the variable ok is 1,
print
}
}' file1 file2https://stackoverflow.com/questions/65732601
复制相似问题