LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 07-08-2008, 02:09 PM   #1
fancylad
Member
 
Registered: Mar 2008
Distribution: slackware
Posts: 175

Rep: Reputation: 19
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
 
Old 07-08-2008, 02:17 PM   #2
trickykid
Guru
 
Registered: Jan 2001
Posts: 24,133

Rep: Reputation: 197Reputation: 197
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
 
Old 07-08-2008, 02:30 PM   #3
fancylad
Member
 
Registered: Mar 2008
Distribution: slackware
Posts: 175

Original Poster
Rep: Reputation: 19
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
 
Old 07-08-2008, 02:36 PM   #4
trickykid
Guru
 
Registered: Jan 2001
Posts: 24,133

Rep: Reputation: 197Reputation: 197
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
 
Old 07-08-2008, 02:43 PM   #5
fancylad
Member
 
Registered: Mar 2008
Distribution: slackware
Posts: 175

Original Poster
Rep: Reputation: 19
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.
 
Old 07-08-2008, 02:51 PM   #6
trickykid
Guru
 
Registered: Jan 2001
Posts: 24,133

Rep: Reputation: 197Reputation: 197
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
 
Old 07-08-2008, 03:55 PM   #7
rjlee
Senior Member
 
Registered: Jul 2004
Distribution: Ubuntu 7.04
Posts: 1,990

Rep: Reputation: 66
Try this:
Code:
diff --line-format=%L file1 file2
 
Old 07-08-2008, 04:34 PM   #8
trickykid
Guru
 
Registered: Jan 2001
Posts: 24,133

Rep: Reputation: 197Reputation: 197
Quote:
Originally Posted by rjlee View Post
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
 
Old 06-15-2013, 12:37 PM   #9
sudon't
LQ Newbie
 
Registered: Jun 2013
Posts: 2

Rep: Reputation: Disabled
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.
 
Old 06-17-2013, 12:35 AM   #10
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,232

Rep: Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024
From the comm man page
Quote:
comm - compare two sorted files line by line
Note the s-word !
 
Old 06-17-2013, 12:50 PM   #11
sudon't
LQ Newbie
 
Registered: Jun 2013
Posts: 2

Rep: Reputation: Disabled
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
print the file: between the lines viveksnv Programming 3 02-26-2008 03:06 PM
getting sed to hold lines and print them out again- help please inonzi_prowler Linux - Software 3 02-21-2007 03:26 AM
Using diff and erasing the lines that match ElectroLinux Linux - Newbie 4 09-20-2006 03:34 AM
awk print lines that doesn't have a pattern huynguye Programming 5 05-04-2006 11:08 AM
Using diff to compare file with common lines, but at different line numbers jimieee Linux - Newbie 3 05-10-2004 07:26 AM


All times are GMT -5. The time now is 07:26 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration