Computing.Net > Forums > Unix > compare two files field string

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

compare two files field string

Reply to Message Icon

Name: ricky007
Date: March 21, 2008 at 08:33:53 Pacific
OS: solaris
CPU/Ram: solaris
Comment:

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.



Sponsored Link
Ads by Google

Response Number 1
Name: ricky007
Date: March 24, 2008 at 17:40:58 Pacific
Reply:

The following code I have tried but getting error on line 9 and 10: Please assist:

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


0

Response Number 2
Name: James Boothe
Date: March 27, 2008 at 08:04:09 Pacific
Reply:

The script you posted seems to be a good approach, but I do not know why you are changing RS and FS from their defaults.

Your script works for me, but with the following changes:

I left RS and FS at their defaults.  You may have special needs here that I am not aware of.

I used the input files from your initial posting, which means my script below picks up the name value from $2 instead of $4.

The only lines I process (in both files) are the name lines.


awk '\

# save first file name fields
NR == FNR {
  prefile = FILENAME
  if ($1 == "name")
     seen[$2] = 1
}

# test if the second file name fields are in the array
NR != FNR {
nextf = FILENAME;
if ($1 == "name")
   if( $2 in seen)
      seen[$2] = 0
   else
      print $2 " found in " nextf " not found in " prefile
}

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



mazda found in file.def not found in file.abc
Isuzu found in file.def not found in file.abc
Ford found in file.abc not found in file.def


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: compare two files field string

compare two files www.computing.net/answers/unix/compare-two-files/8438.html

compare two files and diff file wil www.computing.net/answers/unix/compare-two-files-and-diff-file-wil/7363.html

comparing two files www.computing.net/answers/unix/comparing-two-files/8256.html