Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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 fffstart prog
name Toyota
ggg abc
ttt fffstart prog
name Ford
ggg abc
ttt fff
File2.bcdstart apps
name mazda
ggg abc
ttt fff
start prog
name cadillac
ggg abc
ttt fffstart prog
name Toyota
ggg abc
ttt fff
start prog
name Isuzu
ggg abc
ttt fffI 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.

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

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

![]() |
![]() |
![]() |

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