36

I have file a and b and I would like to output lines of b that changed since it was cloned from a. Just the modified lines, no surrounding context, no diff offset marks.

How can I do that using shell scripting? (No Python/Perl/PHP/...)

Sed and awk are acceptable solutions.

For now, what I am doing is diff -y with --suppress-common-lines and sed using regex backreferences to just fetch the right part after the whitespace. There must be a better way?

Using perl (which is forbidden), I´d do something like this:

diff -y --suppress-common-lines -W $COLUMNS Eclipse_Preferences_Export_*.epf | perl -pe 's/.*\t|\t(.*)$/\1/g'

3 Answers 3

53

With GNU diffutils package's diff this will output only lines from file b which either were modified or newly inserted:

diff --unchanged-line-format= --old-line-format= --new-line-format='%L' a b
1
  • 6
    Awesome. Shame the info is buried in the Texinfo and no hint is provided in the man page I was reading.. Nov 25, 2012 at 15:05
6

You have to add one more option :

grep -vf file1 file2
3
  • 1
    While this doesn't require the lines to be sorted, I think grep uses a lot more memory and will crash for large files. Aug 17, 2017 at 4:14
  • doesn't work if lines are duplicated either
    – Jayen
    Nov 2, 2018 at 9:05
  • 1
    incorrect answer, the correct use of grep is this: grep -vxF -f file1 file2 Sep 29, 2021 at 4:33
4
awk 'FNR==NR{old[$0];next};!($0 in old)' old.txt new.txt

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .