[英]Awk comparison on files
I have 2 files:我有2个文件:
file1:文件 1:
1,apple
2,mango
3,banana
44,orange
file2:文件2:
1,apple
22,
31,xyz
2,man
3,banana
44,oran
44,orange
I need to find the differences from both the files using column 1 and checking column 2. I don't want to use $0 as its printing the lines which of 1st file which are not present in file 2 too.我需要使用第 1 列和检查第 2 列找出这两个文件的差异。我不想使用 $0 来打印第 1 个文件中第 2 个文件中不存在的行。
result output should be printed in file3 as :结果输出应在 file3 中打印为:
2,mango,man
as of now in other question, I asked and got an answer as:-截至目前,在其他问题中,我问并得到了以下答案:-
{ awk 'BEGIN{FS=OFS=","}($1 in a) && a[$1]!=$2{print $1,a[$1],$2}{a[$1]=$2}' file1 file2 >> file3 }
the issue with this solution is that its printing wrong entries in file 3 due to duplicates present in file 2 (for column 1).此解决方案的问题在于,由于文件 2(第 1 列)中存在重复项,因此在文件 3 中打印错误条目。
I need to write these duplicates in file 4 and should not be reflected in file3.我需要在文件 4 中写入这些重复项,不应反映在文件 3 中。
For joining files based on unique records from file 2用于根据文件 2 中的唯一记录连接文件
join -t',' <(sort -t',' -k1 file1) <(sort -t',' -n -k1 -u file2) | awk -F',' '{if($2!=$3) print}'
For finding duplicate entry in file2用于在 file2 中查找重复条目
awk -F',' 'seen[$1]++
Demo :演示:
$man uniq
$cat fil*
1,apple
2,mango
3,banana
44,orange
1,apple
22,
31,xyz
2,man
3,banana
44,oran
44,orange
$join -t',' <(sort -t',' -k1 file1) <(sort -t',' -n -k1 -u file2) | awk -F',' '{if($2!=$3) print}'
2,mango ,man
44,orange,oran
$
$awk -F',' 'seen[$1]++' file2
44,orange
$
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.