I want to do a very simple thing. I have two files as follows:
FILE 1:
A s1 p1
B s2 p2
C s3 p3
FILE2:
B s4 p4
A s1 p1
C s6 p6
I want to extract first and third column from both file and print diff of that file. One easy way is to create intermediate files with cut -f1,3 of both files and do diff. Thats what exactly i want my output is. But i don't want to create intermediate file. Any simple one liner to do that.
One more thing, both the files are NOT sorted, so unable to use join directly.
We can compare the files with this command. Type diff , a space, the name of the first file, a space, the name of the second file, and then press Enter.
Explanation: Setting field separator as , or . for all lines in Input_file and then simply comparing $1 and $5 , for matching case using == condition and for NON matching case using !=
diff stands for difference. This command is used to display the differences in the files by comparing the files line by line. Unlike its fellow members, cmp and comm, it tells us which lines in one file have is to be changed to make the two files identical.
Try this:
diff <(cut -f1,3 file1) <(cut -f1,3 file2)
References:
Compare two files line by line and generate the difference in another file
Use [ process substitution ]
diff -y <( awk '{print $1,$3}' file1) <( awk '{print $1,$3}' file2 )
should do it. Note -y
option with diff
is for side-by-side o/p.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With