13

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.

Shweta
  • 1,111
  • 3
  • 15
  • 30

2 Answers2

14

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

Community
  • 1
  • 1
xealits
  • 4,224
  • 4
  • 27
  • 36
  • 3
    yep, it's called process substitution in bash, as sjsam posted; in fish shell you use `psub`: `diff (cut -f1,3 file1 | psub) (cut -f1,3 file2 | psub)` (and you don't have `>(...)` yet) – xealits Jul 14 '16 at 11:45
  • Additionally, I had to specify the delimiter. So, `diff <(cut -d " " -f1,3 file1.txt) <(cut -d " " -f1,3 file2)` – Charalamm Feb 16 '23 at 08:28
3

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.

sjsam
  • 21,411
  • 5
  • 55
  • 102