0

I have two files. I want to sort the file and join on the first column. For example:

First file:

100.   ttt
200.   616te
300.   7a27
       7373qq

second file:

100.   ttt
200.   yoga
300.   7a27
       8371gd

In order to join the two files I do the following:

join <(sort first_file.txt) <(sort second_file.txt) >joined.txt

I want the resulting output to be:


100.   ttt.      ttt
200.   616te.    yoga
300.   7a27.     7a27
       7373qq.   8371gd

And then I want to see the lines where column 2 and column 3 are different. For that I do:

awk '$2!=$3 {print $1, $2, $3}' joined.txt >different.txt

As you can see some of the values in the first two files are null. And therefore, when I use join I get the following error for example:

join: /dev/fd/63:26456: is not sorted: 100  7373qq
join: /dev/fd/62:23295: is not sorted: 100  8371gd

How can I take care of this? Insights will be appreciated.

4
  • Welcome to the site. Do you need the intermediate file joined.txt for any other purposes, or would you also be happy with a solution that simply compares the "key-value"-associations of both input files and prints the differences?
    – AdminBee
    Jul 2 at 13:29
  • Thank you! Solution that compares key-value association will also be fine.
    – John
    Jul 2 at 13:46
  • 1
    ... and what should happen for the case(s) where the key is empty? also, how are the columns of your files delimited? Jul 2 at 14:46
  • Yes they are delimited by a space. And if key is empty then I want to see if the corresponding value is present in both files. If it is then that won't count as a difference.
    – John
    Jul 2 at 14:47
1

If this isn't all you need then edit your question to provide more truly representative sample input and expected output including cases where this doesn't do what you want:

$ paste file1 file2 | sed 's/\t[^ ]* */.\t/'
100.   ttt.     ttt
200.   616te.   yoga
300.   7a27.    7a27
       7373qq.  8371gd

If you need the above intermediate output as well as the final comparison output then:

$ paste file1 file2 | sed 's/\t[^ ]* */.\t/' | tee intermediate | awk '($NF".")!=$(NF-1)'
200.   616te.   yoga
       7373qq.  8371gd

or if you don't then just:

$ paste file1 file2 | awk '{sub(/\t[^ ]* */,".\t")} ($NF".")!=$(NF-1)'
200.   616te.   yoga
       7373qq.  8371gd

or if you don't actually need a . added at the end of the 2nd field values:

$ paste file1 file2 | awk '{sub(/\t[^ ]* */,"\t")} $NF!=$(NF-1)'
200.   616te    yoga
       7373qq   8371gd

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.