I have 2 files:
file1:
1,apple
2,mango
3,banana
44,orange
file2:
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.
result output should be printed in file3 as :
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).
I need to write these duplicates in file 4 and should not be reflected in file3.
For joining files based on unique records from file 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
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
$
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.