LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-16-2012, 04:20 PM   #1
rafiki8
LQ Newbie
 
Registered: Dec 2008
Posts: 11

Rep: Reputation: 0
Formatting output of diff command


Hello, I'm having problems with the output of the diff command. I'm doing something like this in a shell script:
/usr/bin/diff -y --suppress-common-lines file1 file2

This produces an output like:
azdatetime.h 1.5 17-JAN-2012 11:19:03 TMCKNIGH | azdatetime.h,v 1.3 2004/04/01 15:42:56 cyang
azdevice.h 1.2 16-JUL-2008 09:59:36 THUTCHIN | az.h,v 1.17 2004/07/19 15:09:40 lthornto
az.h 1.21 30-JUN-2008 23:52:14 THUTCHIN | azi18n.h,v 1.5 2004/04/27 14:03:16 cgeigel
azi18n.h 1.7 15-APR-2010 11:11:14 DCAIN <
azstore.h 1.15 10-MAR-2009 13:25:38 JMARX | azstore.h,v 1.10 2004/06/21 16:03:10 rgajula
azstring.h 1.8 09-APR-2010 06:16:58 DCAIN | azstring.h,v 1.5 2004/04/01 15:43:58 cyang
azui.h 1.10 29-JUL-2008 09:02:19 MROBSON | azui.h,v 1.6 2004/08/18 21:56:39 jwilliam

I use this information to compare versions of the files, doing a cut of the fields I'm interested in, but as you can see, when there is one module that is not present in file2 (azdevice.h for example), the output is moved, so for example, now line azi18n.h from file1 doesn't correspond anymore to the one showing for file2.

I need to compare versions of the same modules but I can't make the output of diff to display lines side by side, but respecting line order.

I hope I was able to explain myself and you have some suggestions to try.

Thanks in advance
 
Old 03-16-2012, 04:52 PM   #2
devUnix
Member
 
Registered: Oct 2010
Posts: 606

Rep: Reputation: 59
As you see above "azi18n.h 1.7 15-APR-2010 11:11:14 DCAIN <" does not have a corresponding line in the second file. So, do you want to include it in your report too or not?

If not then, you can exclude such lines:

Code:
/usr/bin/diff -y --suppress-common-lines file1 file2 | grep -v '<$'
or conversely you can include only those lines that have output from both files and joined with the pipe symbol as shown in your output above:

Code:
/usr/bin/diff -y --suppress-common-lines file1 file2 | grep ' | '
 
Old 03-16-2012, 05:06 PM   #3
rafiki8
LQ Newbie
 
Registered: Dec 2008
Posts: 11

Original Poster
Rep: Reputation: 0
Thanks for the quick reply devUnix.

I do need lines that are not present in one of the two files, I'll do something like New Module found or Module deleted with those. Basically what I'm having problems with is the non correspondence of lines with similar output.

I'm trying to do something like this:
azdatetime.h 1.5 17-JAN-2012 11:19:03 TMCKNIGH | azdatetime.h,v 1.3 2004/04/01 15:42:56 cyang
azdevice.h 1.2 16-JUL-2008 09:59:36 THUTCHIN | az.h,v 1.17 2004/07/19 15:09:40 lthornto
az.h 1.21 30-JUN-2008 23:52:14 THUTCHIN | azi18n.h,v 1.5 2004/04/27 14:03:16 cgeigel
azi18n.h 1.7 15-APR-2010 11:11:14 DCAIN <
azstore.h 1.15 10-MAR-2009 13:25:38 JMARX | azstore.h,v 1.10 2004/06/21 16:03:10 rgajula
azstring.h 1.8 09-APR-2010 06:16:58 DCAIN | azstring.h,v 1.5 2004/04/01 15:43:58 cyang
azui.h 1.10 29-JUL-2008 09:02:19 MROBSON | azui.h,v 1.6 2004/08/18 21:56:39 jwilliam

Into something like this:
azdatetime.h 1.5 17-JAN-2012 11:19:03 TMCKNIGH | azdatetime.h,v 1.3 2004/04/01 15:42:56 cyang
azdevice.h 1.2 16-JUL-2008 09:59:36 THUTCHIN <
az.h 1.21 30-JUN-2008 23:52:14 THUTCHIN | az.h,v 1.17 2004/07/19 15:09:40 lthornto
azi18n.h 1.7 15-APR-2010 11:11:14 DCAIN | azi18n.h,v 1.5 2004/04/27 14:03:16 cgeigel
azstore.h 1.15 10-MAR-2009 13:25:38 JMARX | azstore.h,v 1.10 2004/06/21 16:03:10 rgajula
azstring.h 1.8 09-APR-2010 06:16:58 DCAIN | azstring.h,v 1.5 2004/04/01 15:43:58 cyang
azui.h 1.10 29-JUL-2008 09:02:19 MROBSON | azui.h,v 1.6 2004/08/18 21:56:39 jwilliam

Probably I cannot do it with diff and need to parse both files manually, but hopefully diff can be my answer.

Thanks
 
Old 03-17-2012, 12:56 PM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Please use [code][/code] tags around your code and data, to preserve formatting and to improve readability. Please do not use quote tags, colors, or other fancy formatting.

I'm not sure I understand your requirements exactly, and I'm not an expert in diff anyway, but you may want to try reading the info page for it, as it goes into much more detail than the man page.

It might also help if you gave us some example input text of the two files you are comparing, as well as how exactly you want the output of it to look.
 
Old 03-20-2012, 12:00 PM   #5
rafiki8
LQ Newbie
 
Registered: Dec 2008
Posts: 11

Original Poster
Rep: Reputation: 0
Ok, I'll try to provide the example of the text files.

File 1 has:
Code:
azcache.h,v 1.3 2004/04/01 15:42:38 cyang
azdatetime.h 1.5 17-JAN-2012 11:19:03 TMCKNIGH
azdevice.h 1.2 16-JUL-2008 09:59:36 THUTCHIN
az.h 1.21 30-JUN-2008 23:52:14 THUTCHIN
azi18n.h 1.7 15-APR-2010 11:11:14 DCAIN
az_locale.h,v 2.2 2004/04/01 15:42:30 cyang
azlock.h,v 1.1 2004/03/22 20:53:45 cyang
azmacros.h,v 1.5 2004/04/01 16:30:34 jgilreat
azsignal.h,v 1.2 2004/04/01 15:43:49 cyang
azstore.h 1.15 10-MAR-2009 13:25:38 JMARX
File 2 has:
Code:
azcache.h,v 1.3 2004/04/01 15:42:38 cyang
azdatetime.h,v 1.3 2004/04/01 15:42:56 cyang
az.h,v 1.17 2004/07/19 15:09:40 lthornto
azi18n.h,v 1.5 2004/04/27 14:03:16 cgeigel
az_locale.h,v 2.2 2004/04/01 15:42:30 cyang
azlock.h,v 1.1 2004/03/22 20:53:45 cyang
azmacros.h,v 1.5 2004/04/01 16:30:34 jgilreat
azsignal.h,v 1.2 2004/04/01 15:43:49 cyang
azstore.h,v 1.10 2004/06/21 16:03:10 rgajula
azstring.h,v 1.5 2004/04/01 15:43:58 cyang
I'm trying this diff command:
Code:
 /usr/bin/diff -y --suppress-common-lines file1 file2
Which produces this output:
Code:
azdatetime.h 1.5 17-JAN-2012 11:19:03 TMCKNIGH                | azdatetime.h,v 1.3 2004/04/01 15:42:56 cyang
azdevice.h 1.2 16-JUL-2008 09:59:36 THUTCHIN                  | az.h,v 1.17 2004/07/19 15:09:40 lthornto
az.h 1.21 30-JUN-2008 23:52:14 THUTCHIN                       | azi18n.h,v 1.5 2004/04/27 14:03:16 cgeigel
azi18n.h 1.7 15-APR-2010 11:11:14 DCAIN                       <
azstore.h 1.15 10-MAR-2009 13:25:38 JMARX                     | azstore.h,v 1.10 2004/06/21 16:03:10 rgajula
                                                              > azstring.h,v 1.5 2004/04/01 15:43:58 cyang
In order to compare the version of the same modules, I need lines with the same module name to be side by side, something like this:

Code:
azdatetime.h 1.5 17-JAN-2012 11:19:03 TMCKNIGH                | azdatetime.h,v 1.3 2004/04/01 15:42:56 cyang
azdevice.h 1.2 16-JUL-2008 09:59:36 THUTCHIN                  < 
az.h 1.21 30-JUN-2008 23:52:14 THUTCHIN                       | az.h,v 1.17 2004/07/19 15:09:40 lthornto
azi18n.h 1.7 15-APR-2010 11:11:14 DCAIN                       | azi18n.h,v 1.5 2004/04/27 14:03:16 cgeigel
azstore.h 1.15 10-MAR-2009 13:25:38 JMARX                     | azstore.h,v 1.10 2004/06/21 16:03:10 rgajula
This way, even if azdevice.h was added to file1, I still should be able to compare the rest of the modules' versions.

I hope this was clearer.

Thanks
 
Old 03-21-2012, 05:59 PM   #6
rafiki8
LQ Newbie
 
Registered: Dec 2008
Posts: 11

Original Poster
Rep: Reputation: 0
I was able to get my require output doing redirecting the output of the regular diff to a file and parsing this file.

This post can be closed.

Thanks
 
  


Reply



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



Similar Threads
Thread Thread Starter Forum Replies Last Post
diff output meaning sloganyart Linux - Software 4 10-13-2009 01:32 AM
Feeding the output of "diff" or "cat" command to dpkg --purge kushalkoolwal Debian 9 06-19-2008 07:27 AM
Question on diff's output just.srad Programming 4 04-03-2008 10:33 PM
How can I use diff to difference between programs output? nadavvin Linux - General 4 08-05-2006 04:35 AM
CLI....ls (or similar command)...formatting output Basslord1124 Linux - Newbie 2 02-27-2005 05:13 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:53 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration