LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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
 
LinkBack 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,121

Rep: Reputation: 121Reputation: 121
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,121

Rep: Reputation: 121Reputation: 121
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,121

Rep: Reputation: 121Reputation: 121
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,989

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

Rep: Reputation: 121Reputation: 121
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
 
  


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 Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are 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 11:28 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
Open Source Consulting | Domain Registration