LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   how to use diff to only print lines (https://www.linuxquestions.org/questions/linux-general-1/how-to-use-diff-to-only-print-lines-654370/)

fancylad 07-08-2008 02:09 PM

how to use diff to only print lines
 
Hi,

How do I use diff to only print out the actual differences between two text files, and not all those numbers? I have been searching through the man page but don't see a way to do it. Perhaps I have missed something.

Thanks

trickykid 07-08-2008 02:17 PM

What version are you using? I'm on Fedora Core 8 and by default it seems it only prints off the lines that are different.

Examples:

file1:
Code:

this is a line
that is only text.

blah

file2
Code:

this is a line
that is only text.

blah 2

Sample output of diff:
Code:

[trickykid@your-momma ~]$ diff file1 file2
4c4
< blah
---
> blah 2


fancylad 07-08-2008 02:30 PM

sorry. i mean only the lines and not those numbers that precede the line. so for your example i would like to see:

Quote:

blah
blah2
and not

Quote:

4c4
< blah
---
> blah 2

trickykid 07-08-2008 02:36 PM

Well, I'd say you want to keep the < and > cause they indicate which file the line resides in. It would get sort of messy looking at more lines with differences.

But to remove the other stuff, you could easily grep them out.

Code:

diff file1 file2 | grep -v "^---" | grep -v "^[0-9c0-9]"
Code:

[trickykid@your-momma ~]$ diff file1 file2 | grep -v "^---" | grep -v "^[0-9c0-9]"
< blah
> blah 2


fancylad 07-08-2008 02:43 PM

Thanks. I already used that grep method but I'm not crazy about using it. I thought that diff would be capable of doing this on its own.

trickykid 07-08-2008 02:51 PM

You could always do the side by side type format which is rather nice and I tend to use it more often, easier to read on smaller files at least:

Code:

[trickykid@your-momma ~]$ diff --suppress-common-lines -y file1 file2
blah                                                          | blah 2


rjlee 07-08-2008 03:55 PM

Try this:
Code:

diff --line-format=%L file1 file2

trickykid 07-08-2008 04:34 PM

Quote:

Originally Posted by rjlee (Post 3208020)
Try this:
Code:

diff --line-format=%L file1 file2

Yeah, forgot about the line format, hadn't been reading the man page but that didn't seem to work for me.

Code:

[trickykid@your-momma ~]$ diff --line-format=%L file1 file2
this is a line
that is only text.

blah
blah 2


sudon't 06-15-2013 12:37 PM

Try comm
 
You might try comm:

Code:

comm -3 file1.txt file2.txt
The -3 flag will cause it to print the lines in the second file that are not common to both files. And you can do it vice-versa. See the man page for details.

chrism01 06-17-2013 12:35 AM

From the comm man page
Quote:

comm - compare two sorted files line by line
Note the s-word !

sudon't 06-17-2013 12:50 PM

Sort, Indeed!
 
Yes, good point! Use the sort command, first.
Heck, as long as you have sort out of the garage, forget comm and diff. Just use the sort | uniq combo with the -u flag:

Code:

sort file1.txt file2.txt |uniq -u > uniq_lines.txt
That'll send only the unique lines to a new file, which I believe is what you want to do.


All times are GMT -5. The time now is 11:04 PM.