6

I am using diff to find difference from current file to a target.

The command I have is diff -w --from-file /tmp/new_list.txt /tmp/protected_client.txt

The output is

8c8,9
> VM-E

I filtered this using: diff -w --from-file /tmp/new_list.txt /tmp/protected_client.txt | tail -n +2 | sed 's/> //g'

Great. Now, If I have multiple differences, the I see:

2d1
< esxi02
3a3
> psc-dr

Two questions. What are these values. I know < means value is there in from-file but not in to-file and > is the reverse,

Second question: how do I get rid of these codes 2d1 and 3a3 or whatever comes up.

1 Answers1

11

Those codes give you a line number and a type of difference which has been found:

  • a stands for added
  • c changed
  • d deleted

For example: 8c8,9 means that line number 8 in the first file (--from-file) has changed to something and this something is stored in the lines 8-9 in the second file. Similarly 2d1 means that line 2 from the first file has been deleted, expected after line 1 in the second file, etc.

I don't think you really want to get rid of those codes, they are quite useful, but if you insist on removing them (or changing to something else), then just play with the following six output formats (here I assume you are using GNU diff):

  • --old-line-format
  • --new-line-format
  • --old-group-format
  • --new-group-format
  • --changed-group-format
  • --unchanged-group-format

I will not cover all details of the (rather complex) syntax here, you will find their description in the info diff under If-then-else chapter. The bellow will give you exactly what you are asking, i.e. no codes at all, just pure < and >:

diff --old-line-format=$'< %l\n' --new-line-format=$'> %l\n' \
     --old-group-format=$'%<' --new-group-format=$'%>' \
     --changed-group-format=$'%>' --unchanged-group-format='' file1 file2

You can make alias of that if you want.

jimmij
  • 47,140