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.

10

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.

  • I feel that if the OP wants a crippled diff, other tools like comm might be an option, instead of attempting to bend diff to their will? – AlwaysLearning Jul 29 '17 at 2:33
  • Yes. Thanks for the comm option. It worked like a charm. I used: comm -3 file2.txt file.txt – suhas savkoor Jul 30 '17 at 17:13

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.