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
# 8  
Old 12-22-2010
Script is working fine now.
Code:
# ./comp
----------------------------- U S A G E -----------------------------

 Usage: comp patches|pkgs

To compare 2 patch lists
   Create your patch list from each server
   showrev -p |cut -d : -f 2 | awk '{ print $1 }' > `hostname`.patches

To compare 2 pkg lists
   Create your package list from each server
   pkginfo -x > `hostname`.pkgs

#
# ./comp pkgs
Enter the name of the first  pkgs file : suntest.pkg
Enter the name of the second pkgs file : hostdb.pkg
----------------------------- installed pkgs -----------------------------
Packages that exist only on suntest.pkg
OPSjass  OPS JASS
         (sparc) 1.5
OPSkeycl  Public Key Distribution - Client Code
          (sparc) 2.0
OPSpmgmt  OPS Password Management Software
          (sparc) 1.5
SUNWcdhe  Simplified Chinese L10N for CDE HELP RUNTIME
          (sparc) 9.0,REV=2003.11.26.19.22
SUNWcdhev  Simplified Chinese CDE HELP VOLUMES
           (sparc) 9.0,REV=2004.01.30.09.07

Packages that exist only on hostdb.pkg
OPSjass  OPS JASS
         (sparc) 1.6
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

#
# ./comp patches
Enter the name of the first  patches file : suntest.patches
Enter the name of the second patches file : hostdb.patches
----------------------------- installed patches -----------------------------
Patches that exist only on suntest.patches
128548-08
139944-01

Patches that exist only on hostdb.patches
137392-01
137394-01

#

Here's the script.
I need to clean it up a bit and add a few more checks, but the bulk of it is done
Code:
#!/bin/ksh
#
# Author: Goran Cvetanoski
#
# comp
#
# This script is used to compare 2 patch or pkg lists
#
# Create your patch list from each server
# showrev -p |cut -d : -f 2 | awk '{ print$1 }' > `hostname`.patches
#
# Create your package list from each server
# pkginfo -x > `hostname`.pkgs
#
# then run this script
#
# CHANGE lOG
# ==================================================================
# Goran     20/12/2010  Initial script created
# Goran     22/12/2010  Add pkg info and add colour
# Goran     23/12/2010  Add pkgs version check
#

YE="\033[1;33m" # bright yellow
GR="\033[1;32m" # bright green
RE="\033[1;31m" # bright red
gy="\033[0;00m" # dull grey

LOG=/tmp/comp.log
LIST1=/tmp/comp1.log
LIST2=/tmp/comp2.log

DASHES="-----------------------------"

show()
{
    echo "$DASHES $1 $DASHES" >> $LOG
    shift
    eval "$@" >> $LOG
    echo "" >> $LOG
}

usage ()
{
    printf "${GR}\n"
    printf " Usage: comp patches|pkgs \n\n"
    printf "${YE}To compare 2 patch lists\n"
    printf "${GR}   Create your patch list from each server\n"
    printf "   showrev -p |cut -d : -f 2 | awk '{ print \$1 }' > \`hostname\`.patches\n\n"
    printf "${YE}To compare 2 pkg lists\n"
    printf "${GR}   Create your package list from each server\n"
    printf "   pkginfo -x > \`hostname\`.pkgs${gy}\n"
}

COMP ()
{
    if [ "$1" == "pkgs" ]; then
        cat $list1 | xargs -L2 > $LIST1
        cat $list2 | xargs -L2 > $LIST2

        printf "${GR}Packages that exist only on $list1${gy}\n"
        installed1=`/usr/xpg4/bin/grep -x -v -f $LIST2 $LIST1 | awk '{ print $1 }'`
        for i in $installed1; do
            cat $list1 | nawk 'c-->0;$0~s{if(b)for(c=b+1;c>1;c--)print r[(NR-c+1)%b];print;c=a}b{r[NR%b]=$ 0}' b=0 a=1 s=$i
        done

        printf "${GR}Packages that exist only on $list2${gy}\n"
        installed2=`/usr/xpg4/bin/grep -x -v -f $LIST1 $LIST2 | awk '{ print $1 }'`
        for i in $installed2; do
            cat $list2 | nawk 'c-->0;$0~s{if(b)for(c=b+1;c>1;c--)print r[(NR-c+1)%b];print;c=a}b{r[NR%b]=$ 0}' b=0 a=1 s=$i
        done
    else
        printf "${GR}Patches that exist only on $list1${gy}\n"
        /usr/xpg4/bin/grep -x -v -f $list2 $list1
        echo ""
        printf "${GR}Patches that exist only on $list2${gy}\n"
        /usr/xpg4/bin/grep -x -v -f $list1 $list2
    fi
}

cat /dev/null > $LOG

case "$1" in
    patches|pkgs)
        echo "Enter the name of the first  $1 file : \c"
        read -r list1
        echo "Enter the name of the second $1 file : \c"
        read -r list2
        printf "${RE}"
        show "installed $1" COMP $1 $list1 $list2;;
    *)
        printf "${RE}"
        show "U S A G E" usage
        cat $LOG
        cat /dev/null > $LOG
        exit 1 ;;
esac

cat $LOG

Tornado
# 9  
Old 12-23-2010
Code:
# for i in file1 file2 ; do diff -u file1 file2 | sed '/---/d;/+++/d;/@@/d;/^  */d'|sed '/SUN/{s/-/file1\
/g;s/+/file2\
/g;}'| sed -ne '/'$i'/{$!N;$!N;h;x;p;}'|sed '1!s/'$i'//;$s/$/\
/;s/'$i'/Packages that exist only in '$i':/'|sed 's/[-+]//g;'; done
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

Regards
@ygemici
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