2

I want to use linux diff command to get the following output:

2,4c2,4 

I only want to know the line numbers where the files are different. I don't want the actual line on the console.

Eg:

If I will execute the following command: diff file1.txt file2.txt

I would like the following output:

2,4c2,4

I don't want the output:

2,4c2,4 < I need to run the laundry. < I need to wash the dog. < I need to get the car detailed. --- > I need to do the laundry. > I need to wash the car. > I need to get the dog detailed. 

I went through the manual of diff command but I wasn't able to find any option that would allow me to achieve what I want.

2
  • A wealth of information can be found in info diff, check it out, I am sure you'll find something Commented Jul 6, 2016 at 17:37
  • hey John Rambo ! Does any of the below answer your question ? Commented Jul 6, 2016 at 17:53

2 Answers 2

5

Pipe it to grep and only show lines beginning with numbers.

diff file1.txt file2.txt | grep '^[1-9]' 
Sign up to request clarification or add additional context in comments.

Comments

1

pass the flag -f .

-sh-4.1$ cat file1.txt I need to run the laundry. I need to wash the dog. difdferen line I need to get the car detailed. -sh-4.1$ cat file2.txt I need to run the laundry. I need to wash the dog. I need to get the car detailed. -sh-4.1$ diff -f file1.txt file2.txt d3 

Edited as per @Barmar comment: for it to work on changed lines .. you can just filter lines with "<" or ">" by asking for the inverse of lines that start with "<" or ">"

First : plain diff :

-sh-4.1$ diff file* 3d2 < difdferen line 4a4 > different line in file2 -sh-4.1$ 

with grep to filter lines that start with < or >

 -sh-4.1$ diff file* | egrep -v "^<|^> |^-" 

3,4d2 5a4

3d2 4a4 

simplified version suggested by @Barmar

-sh-4.1$ diff file1.txt file2.txt | egrep -v "^[-<>]" 3,4d2 5a4 

7 Comments

That works for deleted lines, not for changed lines.
and also not for added lines
Thanks @Barmar for the feedback . updated the answer .
is --- a line or just something that separates the diff of the two files ? not sure
You can simplify it to ^[-<>]
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.