Reputation: 305
I am comparing two files file1 and file2, i need to print the updated records of file2 which am comparing in file1. i need data changes of file2 and newly added records
File1:
1|footbal|play1
2|cricket|play2
3|tennis|play3
5|golf|play5
File2:
1|footbal|play1
2|cricket|play2
3|tennis|play3
4|soccer|play4
5|golf|play6
output file:
4|soccer|play4
5|golf|play6
i have tried the below solution but its not the expected output
awk -F'|' 'FNR == NR { a[$3] = $3; a[$1]=$1; next; } { if ( !($3 in a) && !($1 in a) ) { print $0; } }' file1.txt file2.txt
i have compared the column1 and column3 from both files
Upvotes: 1
Views: 1618
Reputation: 133518
Could you please try following.
awk 'BEGIN{FS="|"}FNR==NR{a[$1,$3];next} !(($1,$3) in a)' Input_file1 Input_file2
OR a non-one liner form of solution.
awk '
BEGIN{
FS="|"
}
FNR==NR{
a[$1,$3]
next
}
!(($1,$3) in a)
' Input_file1 Input_file2
Explanation: Adding detailed explanation for above code.
awk ' ##Starting awk program from here.
BEGIN{ ##Starting BEGIN section of this program from here.
FS="|" ##Setting FS as pipe here as per Input_file(s).
} ##Closing BEGIN block for this awk code here.
FNR==NR{ ##Checking condition FNR==NR which will be TRUE when 1st Input_file named file1 is being read.
a[$1,$3] ##Creating an array named a with index os $1,$3 of current line.
next ##next will skip all further statements.
}
!(($1,$3) in a) ##Checking condition if $1,$3 are NOT present in array a then print that line from Input_file2.
' Input_file1 Input_file2 ##mentioning Input_file names here.
Output will be as follows.
4|soccer|play4
5|golf|play6
Upvotes: 2