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 and shell scripting languages 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 11:34 AM
compare two files charandevu Shell Programming and Scripting 7 03-30-2008 04:20 PM
Compare files kharen11 UNIX for Advanced & Expert Users 25 03-14-2007 05:35 AM
compare files and beyond MizzGail UNIX for Dummies Questions & Answers 2 04-25-2003 02:34 PM
compare files ingunix UNIX for Dummies Questions & Answers 3 05-24-2001 12:44 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-21-2008
amir07 amir07 is offline
Registered User
  
 

Join Date: Feb 2007
Posts: 37
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.
  #2 (permalink)  
Old 03-21-2008
yunccll yunccll is offline
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 05:25 PM.. Reason: fix an expression error! :-)
  #3 (permalink)  
Old 03-24-2008
amir07 amir07 is offline
Registered User
  
 

Join Date: Feb 2007
Posts: 37
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
  #4 (permalink)  
Old 03-24-2008
summer_cherry summer_cherry is online now Forum Advisor  
Registered User
  
 

Join Date: Jun 2007
Location: Beijing China
Posts: 1,088
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
  #5 (permalink)  
Old 03-25-2008
amir07 amir07 is offline
Registered User
  
 

Join Date: Feb 2007
Posts: 37
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
  #6 (permalink)  
Old 03-25-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
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.
Closed Thread

Bookmarks

Tags
linux

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 04:10 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0