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

Computing.Net: Over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to sign up now, it's free!

compare two files field string

Reply to Message Icon

Original Message
Name: ricky007
Date: March 21, 2008 at 08:33:53 Pacific
Subject: compare two files field string
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.


Report Offensive Message For Removal


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

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


Report Offensive Follow Up For Removal

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

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


Report Offensive Follow Up For Removal







Use following form to reply to current message:

Not Logged In!

You will not be able to post.

Register | Login
   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: compare two files field string

Comments:



  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 





Do you have your own blog?

Yes
No
I did before
I will soon


View Results

Poll Finishes In 2 Days.
Discuss in The Lounge
Poll History




Data Recovery Software