4

I want to find out what was added to a log file between 2 tasks. Right now the way I do it is:

content of test.log initially

aaa
bbb

saving content of the log

$ cp test.log test.log.before

doing something

content of test.log after doing something

aaa
bbb
ccc

saving content of the log

$ cp test.log test.log.after

showing the difference

$ diff test.log.before test.log.after
2a3
> ccc

What I don't like with this method is that I get some noise from diff (2a3 and >) whereas I just want ccc.

Q: Is there a way I can just get the difference between the 2 log files (i.e. what is new in test.log.after, ccc)?

I had a look at diff's man page but I don't see anything obvious there.

Max
  • 3,523
  • 16
  • 53
  • 71

2 Answers2

4

If you always do a before and after such that the end of the after file will contain the new content then you could use comm.

$ cat before 
aaa
bbb

$ cat after
aaa
bbb
ccc

$ comm -3  after before
ccc
user9517
  • 115,471
  • 20
  • 215
  • 297
  • But, the files should be sorted! – Khaled Nov 29 '11 at 12:17
  • This works as expected if the files are the same up to the point that new stuff is added. – user9517 Nov 29 '11 at 12:36
  • `comm` is OK but output comes out tabulated. I prefer the `grep`+`cut` alternative. – Max Nov 29 '11 at 12:44
  • that's why I did `after before` so that the output (which contains the changes) is at the beginning of the line, doing `before after` would give you tabs to deal with. – user9517 Nov 29 '11 at 12:49
3

If you want to get the additions only you can filter the output such as:

$ diff test.log.before test.log.after | grep "^>" | cut -c 3-

However, you will lose the deletions if any.

Khaled
  • 36,533
  • 8
  • 72
  • 99