![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Rules & FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| 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 |
|
|
LinkBack | Thread Tools | Display Modes |
|
|||
|
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. |
| Forum Sponsor | ||
|
|
|
|||
|
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! :-) |
|
|||
|
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
|
|
|||
|
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. |