Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

diff on columns of two files in shell

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.

like image 565
Shweta Avatar asked Jul 14 '16 11:07

Shweta


People also ask

How do I find the difference between two files in terminal?

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.

How do I compare two columns in awk?

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 !=

What is diff in shell script?

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.


2 Answers

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

like image 131
xealits Avatar answered Oct 22 '22 22:10

xealits


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.

like image 22
sjsam Avatar answered Oct 22 '22 21:10

sjsam