Compare 2 files, 2 lines at a time


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Compare 2 files, 2 lines at a time
# 1  
Old 12-22-2010
Compare 2 files, 2 lines at a time

Hi Guys,
I want to find any differences between packages installed on 2 servers/zones.
I have 2 files that contain the output from pkginfo -x . I want to know if any packages exist only in one file and I want to also know about any packages that exist in both but with a different version.

ie:
If I have these 2 files.
Code:
file1:
SUNWzlib  The Zip compression library
          (sparc) 11.10.0,REV=2005.01.08.05.16
SUNWzoner  Solaris Zones (Root)
           (sparc) 11.10.0,REV=2005.01.21.15.53
SUNWzoneu  Solaris Zones (Usr)
           (sparc) 11.10.0,REV=2005.01.21.15.53
SUNWjfca  JNI Fibre Channel Adapter (FCA) Driver
          (sparc) 11.10.0,REV=2005.01.04.02.17

file2:
SUNWzoner  Solaris Zones (Root)
           (sparc) 11.10.0,REV=2005.01.21.15.53
SUNWzlib  The Zip compression library
          (sparc) 12.10.0,REV=2005.01.08.05.16
SUNWzoneu  Solaris Zones (Usr)
           (sparc) 11.10.0,REV=2005.01.21.15.53

I want the compare script to return this:
Code:
# compare file1 file2

Packages that exist only in file1:
SUNWzlib  The Zip compression library
          (sparc) 11.10.0,REV=2005.01.08.05.16

SUNWjfca  JNI Fibre Channel Adapter (FCA) Driver
          (sparc) 11.10.0,REV=2005.01.04.02.17

Packages that exist only in file2:
SUNWzlib  The Zip compression library
          (sparc) 12.10.0,REV=2005.01.08.05.16

Currently I have a script that identifies missing package, but not packages that exist on both servers, but with different versions.

SPARC platform running Solaris 10.
Tornado
# 2  
Old 12-22-2010
First of all format both the files like this.

Code:
sed 's/^  *//g' file1 | xargs -d"|" -n2 | sort > formattedFile1
sed 's/^  *//g' file2 | xargs -d"|" -n2 | sort > formattedFile2

This will show following output.

Code:
formattedFile1
SUNWzlib  The Zip compression library|(sparc) 11.10.0,REV=2005.01.08.05.16
SUNWzoner  Solaris Zones (Root)|(sparc) 11.10.0,REV=2005.01.21.15.53
SUNWzoneu  Solaris Zones (Usr)|(sparc) 11.10.0,REV=2005.01.21.15.53
SUNWjfca  JNI Fibre Channel Adapter (FCA) Driver|(sparc) 11.10.0,REV=2005.01.04.02.17

formattedFile2
SUNWzoner  Solaris Zones (Root)|(sparc) 11.10.0,REV=2005.01.21.15.53
SUNWzlib  The Zip compression library|(sparc) 12.10.0,REV=2005.01.08.05.16
SUNWzoneu  Solaris Zones (Usr)|(sparc) 11.10.0,REV=2005.01.21.15.53

Now u can simply compare 2 files using grep command.

Code:
grep -x -v -f formattedFile1 formattedFile2          # Lines exists only in File2
grep -x -v -f formattedFile2 formattedFile1          # Lines exists only in File1

R0H0N
# 3  
Old 12-22-2010
Take a look at comm command:
Code:
# comm -23 file1 file 2
SUNWzlib  The Zip compression library
          (sparc) 11.10.0,REV=2005.01.08.05.16
SUNWjfca  JNI Fibre Channel Adapter (FCA) Driver
          (sparc) 11.10.0,REV=2005.01.04.02.17
 
# comm -13 file1 file2
SUNWzlib  The Zip compression library
          (sparc) 12.10.0,REV=2005.01.08.05.16

# 4  
Old 12-22-2010
Once again, thanks guys. I will look at both suggestions

---------- Post updated at 07:46 AM ---------- Previous update was at 06:59 AM ----------

comm doesn't return correct values, so I think I will re format the file from 2 lines into one, but the version of xargs on my systems don't have the -d flag
Code:
# sed 's/^  *//g' file1 | xargs -d "|" -n2 | sort > formattedFile1
xargs: illegal option -- d
xargs: Usage: xargs: [-t] [-p] [-e[eofstr]] [-E eofstr] [-I replstr] [-i[replstr]] [-L #] [-l[#]] [-n # [-x]] [-s size] [cmd [args ...]]

Any other ways to reformat the file ?

---------- Post updated at 07:57 AM ---------- Previous update was at 07:46 AM ----------

I should add, I want to use it in a ksh script
Tornado
# 5  
Old 12-22-2010
comm command given by Klashxx should give the output you mentioned in post #1.
Packages that exist only in file1:
Code:
SUNWzlib  The Zip compression library
          (sparc) 11.10.0,REV=2005.01.08.05.16

SUNWjfca  JNI Fibre Channel Adapter (FCA) Driver
          (sparc) 11.10.0,REV=2005.01.04.02.17

Packages that exist only in file2:
Code:
SUNWzlib  The Zip compression library
          (sparc) 12.10.0,REV=2005.01.08.05.16

# 6  
Old 12-22-2010
It works OK for those files, but doesn't work as expected when used with pkginfo -x to generate the files.

Anyway I think I will use grep after I have reformatted the file with awk
Code:
# cat file1
SUNWced  Sun GigaSwift Ethernet Adapter Driver
         (sparc.sun4u) 1.0,REV=2004.12.24.10.0
SUNWcxfnt  Simplified Chinese (EUC) X Windows Platform Required Fonts
           (sparc) 10.0,REV=2003.12.09.14.34
SUNWescha  Sun Management Center Common Config Reader Module Sun Fire V440/V445 Agent Component
           (sparc) 3.6-v7,REV=2.10.2007.09.10
SUNWzlib  The Zip compression library
          (sparc) 11.10.0,REV=2005.01.08.05.16
SUNWnfsskr  Network File System (NFS) server kernel support (Root)
            (sparc) 11.10.0,REV=2005.01.21.15.53
SUNWcdhev  Simplified Chinese CDE HELP VOLUMES
           (sparc) 9.0,REV=2004.01.30.09.07
SUNWzoner  Solaris Zones (Root)
           (sparc) 11.10.0,REV=2005.01.21.15.53
SUNWzoneu  Solaris Zones (Usr)
           (sparc) 11.10.0,REV=2005.01.21.15.53
#
# cat file1 | xargs -L 2
SUNWced Sun GigaSwift Ethernet Adapter Driver (sparc.sun4u) 1.0,REV=2004.12.24.10.0
SUNWcxfnt Simplified Chinese (EUC) X Windows Platform Required Fonts (sparc) 10.0,REV=2003.12.09.14.34
SUNWescha Sun Management Center Common Config Reader Module Sun Fire V440/V445 Agent Component (sparc) 3.6-v7,REV=2.10.2007.09.10
SUNWzlib The Zip compression library (sparc) 11.10.0,REV=2005.01.08.05.16
SUNWnfsskr Network File System (NFS) server kernel support (Root) (sparc) 11.10.0,REV=2005.01.21.15.53
SUNWcdhev Simplified Chinese CDE HELP VOLUMES (sparc) 9.0,REV=2004.01.30.09.07
SUNWzoner Solaris Zones (Root) (sparc) 11.10.0,REV=2005.01.21.15.53
SUNWzoneu Solaris Zones (Usr) (sparc) 11.10.0,REV=2005.01.21.15.53
#

Tornado
# 7  
Old 12-22-2010
ok.. This may help. Put TABSPACE in sed command. Assuming 2nd line of every package starts with a TABSPACE and (sparc). Also in sed, <TABSPACE>&/', should be put in new line
sed is just to get output in same way as it is in file1 and file2.
If we remove sed, then package record will be displayed same line (As in tempfile1 and tempfile2)

Code:
 
cat file1 | xargs -L 2 > tempfile1
cat file2 | xargs -L 2 > tempfile2
#print packages which are ONLY in file1
comm -23 tempfile1 tempfile2 | sed 's/(sparc)/\
<TABSPACE>&/'
#print packages which are ONLY in file2
comm -13 tempfile1 tempfile2 | sed 's/(sparc)/\
<TABSPACE>&/'
#print packages which are in both file1 and file2 but with different versions.
awk -F'\(sparc\)' 'NR==FNR{a[$1]=$2;next;}{if(a[$1] && a[$1]!=$2) print $0}' tempfile1 tempfile2 | sed 's/(sparc)/\
<TABSPACE>&/'


Last edited by anurag.singh; 12-22-2010 at 05:58 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Compare lines between two files

I have two files I need to compare these two files and take the lines that are common in both the files and consider the line present in second file for my further processing I have used "Awk" along with "FNR and NR" but that is not working gawk -F= ' > FNR==NR {a=$1; next}; > ... (2 Replies)
Discussion started by: Priya Amaresh
2 Replies

2. UNIX for Dummies Questions & Answers

Compare lines in 2 files

I have 2 files with exactly the same information (with header and separated by ";") and what I would like to do is print (for both files!) the columns that are different and also print the "key" column that is equal in the 2 files For example, if File1: key1;aaa;bbb;ccc key2;ddd;eee;fff... (4 Replies)
Discussion started by: mvalonso
4 Replies

3. Shell Programming and Scripting

Compare files by lines and columns

Inspired by the extremely short awk code from Ygor on this post I wanted to compare two files on only one field. I can't get it to work. Can anybody help on explaining the code and fix the code? My code which does not work: awk 'BEGIN{a=1};a!=1' file1.txt file2.txt >outfile.txt file1.txt... (1 Reply)
Discussion started by: sdf
1 Replies

4. Shell Programming and Scripting

compare date and time inside data of two files

i have two files with identical no of columns. 6th columns is date (MM/DD/YY format) and 7th columns is time (HH:MM:SS) format. I need to compare these two vaules and if the date & time is higher than fileA, save it on fileC; if the value is lower, then save it on fileD CONDITIONS... (7 Replies)
Discussion started by: ajiwww
7 Replies

5. Shell Programming and Scripting

time from 2 files to compare

In first file say first.txt, i have a content say 14:56. In second file, say second.txt i have content say 16:01.... I want to compare if these two times in these 2 files are having a difference of 15 minutes...Can any one please help? (2 Replies)
Discussion started by: manoj.b
2 Replies

6. Shell Programming and Scripting

compare two files and to remove the matching lines on both the files

I have two files and need to compare the two files and to remove the matching lines from both the files (4 Replies)
Discussion started by: shellscripter
4 Replies

7. Shell Programming and Scripting

compare files by lines and columns

Dear All, Is it possible to compare 2 files line to line using column values? for example I have file1: 1;givi;01012000;wer 2;sss;02012000;rrr 3;ccc;03012000;ttt file 2: 0;uuu;01012000;lll 1;givi;01012000;wer 2;sss;02012000;rrr 3;ccc;03012000;ttt 5;givi;01012000;hhh I want... (4 Replies)
Discussion started by: giviut
4 Replies

8. Shell Programming and Scripting

Trying to compare lines in 2 files

Hello, I am new to scripting and need some help. In looking at other posts on this forum, I came up with the following logic. I cannot figure out why I am getting names of files of the current directory in my echo output. Scenario: message file has a line containing the version. Version.txt... (2 Replies)
Discussion started by: brdholman
2 Replies

9. UNIX for Dummies Questions & Answers

compare update time of files

Hi, does anyone know of a way to compare files update time (not only days - also hours and minutes) (command? scripts? perl scripts?) Dori (8 Replies)
Discussion started by: dorilevy
8 Replies

10. UNIX for Dummies Questions & Answers

compare files acess time

I need to compare x file with y file. If x file is newer than y file do xxxx; (I used touch -t yymmddxxx to chnage the time for y file) How can i do that? I tried use if then echo "Xxx";; fi But cant....any other good ways to do it? (2 Replies)
Discussion started by: AkumaTay
2 Replies
Login or Register to Ask a Question