Search code examples
linuxseddiff

How to get only difference from diff


I have this code , how to improve it

diff  -b -i -w  out.txt out2.txt  
 | grep '^>' 
 | sed 's/^>//g' 
 | sed ':a;N;$!ba;s/\n/ /g' 

example data (out.txt)

abc def ghk
abc def2 ghk
abc def ghk

123 333 555
566 3423 23
566 3423 3542

example data (out2.txt)

abc def2 ghk
abc def ghk
abc def ghk

123 555 555
fsdjhfsda sd
566 3423 3542

expected result :

abc def ghk  123 555 555  fsdjhfsda sd

Solution

  • Here's one simple way:

    echo `diff -biw out.txt out2.txt | sed -n 's/^> //p'`
    

    If you want to use use grep alone -- and if you can take advantage of GNU grep's formatting features -- you could use this:

    diff --unchanged-group-format= --changed-group-format=%\> \
      --new-line-format='%l ' -biw out.txt out2.txt
    

    which is probably the fastest method, and it doesn't require a large memory buffer when your files are big. (Both your original sed solution and the echo solution would keep lines in memory until the end, but this will spit them out as it goes.) The command itself is pretty verbose, though. Note also that it leaves one extra space at the end of the output.