The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
compare 2 files.. amon Shell Programming and Scripting 8 06-23-2008 07:34 AM
compare two files charandevu Shell Programming and Scripting 7 03-30-2008 12:20 PM
Compare files kharen11 UNIX for Advanced & Expert Users 25 03-14-2007 01:35 AM
compare files and beyond MizzGail UNIX for Dummies Questions & Answers 2 04-25-2003 10:34 AM
compare files ingunix UNIX for Dummies Questions & Answers 3 05-24-2001 08:44 AM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-21-2008
Registered User
 

Join Date: Feb 2007
Posts: 11
Compare two files

Kindly please assit me to compare two files in perl or any other script:


I have two files:

File1.abc

start prog
name cadillac
ggg abc
ttt fff

start prog
name Toyota
ggg abc
ttt fff

start prog
name Ford
ggg abc
ttt fff


File2.bcd

start apps
name mazda
ggg abc
ttt fff


start prog
name cadillac
ggg abc
ttt fff

start prog
name Toyota
ggg abc
ttt fff


start prog
name Isuzu
ggg abc
ttt fff



I have tried sdiff and diff but the output brings all kind of junks. I am specifically need to
compare just the "name" field.


The script only look for match written next to name field and will create an output telling me unmatch in name field.

So output will say:

name Ford found in file1.abc not found in File2.bcd
name mazda found in file2.bcd not found in File1.abc
name Isuzu found in file2.bcd not found in File1.abc


Thanks and appreciate your help.
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 03-21-2008
Registered User
 

Join Date: Mar 2008
Posts: 23
using awk (linux + bash):
Code:
awk ' BEGIN {RS = ""; FS = "(\n)| ( )"}  # set the record separator as ""

        # save the first file name and 4th field of it in an array 
        NR == FNR {   seen[$4] = 1; prefile = FILENAME }

        # test if the $4 in second file existing in the array or not
        NR != FNR  {
          nextf = FILENAME;
          if( $4 in seen)   seen[$4] = 0
          else      
             print $4 " found in " nextf " not found in " prefile
        }

        # print the field in first file but not in the nextfile
        END {
          for( ix in seen){
            if( seen[ix] == 1)
              print ix " found in " prefile " not found in " nextf
          }
        }
       '  file.abc  file.def

.Aaron

Last edited by yunccll; 03-21-2008 at 01:25 PM. Reason: fix an expression error! :-)
Reply With Quote
  #3 (permalink)  
Old 03-24-2008
Registered User
 

Join Date: Feb 2007
Posts: 11
Thanks, but I am getting error:

$ ./Compare2NCFiles
awk: syntax error near line 9
awk: illegal statement near line 9
awk: illegal statement near line 10
Reply With Quote
  #4 (permalink)  
Old 03-24-2008
Registered User
 

Join Date: Jun 2007
Posts: 350
Hi,

Try follow one, it should be ok for you!

Code:
sed -n '/^name/p' a | awk '{print $2}' | sort > a.t
sed -n '/^name/p' b | awk '{print $2}' | sort > b.t
for i in `comm -23 a.t b.t`
do
echo "$i" is in a but not in b
done
echo
for i in `comm -13 a.t b.t`
do
echo "$i" is in b but not in a
done
rm a.t b.t
Reply With Quote
  #5 (permalink)  
Old 03-25-2008
Registered User
 

Join Date: Feb 2007
Posts: 11
I am confused now, should I replace this code with previous "awk" code.
In the second code I don't see the file name.

Please assist.....


Thanks
Reply With Quote
  #6 (permalink)  
Old 03-25-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 2,928
It assumes the files to compare are called a and b. It is self-contained, basically a demo of how you would solve this with the "comm" command and related utilities. (sed | awk should be refactored to a single command, and the backticks should probably be avoided; otherwise it's pretty much the textbook solution.)

If you have mawk or nawk or gawk, try yunccll's script with one of those instead.
Reply With Quote
  #7 (permalink)  
Old 03-26-2008
Registered User
 

Join Date: Feb 2007
Posts: 11
Thanks it is working perfect. Appreciate your help.
Reply With Quote
Google UNIX.COM
Reply

Tags
linux

Thread Tools
Display Modes




All times are GMT -7. The time now is 09:47 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0