LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Formatting output of diff command (https://www.linuxquestions.org/questions/linux-newbie-8/formatting-output-of-diff-command-934850/)

rafiki8 03-16-2012 04:20 PM

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

devUnix 03-16-2012 04:52 PM

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 ' | '

rafiki8 03-16-2012 05:06 PM

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

David the H. 03-17-2012 12:56 PM

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.

rafiki8 03-20-2012 12:00 PM

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

rafiki8 03-21-2012 05:59 PM

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


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