Awk Comparison of 2 specific files

Hi Everybody,

I know the topic sounds familiar but I just couldn't adapt or find the right code that solves my particular issue. I really hope you can help.

I would like to compare 2 files in an awk script. Both files have different paths.

The awk script call should look like that awk -f AWKSCRIPT parameter1.

The files look as follows:

File_old

Header
record1
record2
record3
Footer

File_new

Header
record1
record2
record3
Footer

First I would like to test if both files have the same amount of records.

If NO, return a code("NOMATCH") that can be further processed by a shell script. AWK Script is supposed to be called from within a bourne shell script.

If YES, please compare just the records Line by Line. I mean compare:

File_old:record1 with File_new:record1
File_old:record2 with File_new:record2

and so on...

If one of those comparisons doesn't match, return again a code("NOMATCH") that can be further processed by the calling bourne shell script. If all of the records match with on another return code("MATCH") to calling bourne shell script.

I hope I didn't describe it to complicated.

I would be very grateful for all your help.

Thanks in advance,

hhoosscchhii

Hi,

I think its better to use shell command 'comm' or 'diff' for this requirment.
It will be like

DiffCount=`comm -3 file1 file2|wc -l`
if [ $DiffCount -gt 0 ]
then
echo "Files are not same"
fi

Regards,

Ranjith

Hi ,

Hope,this also can help you....

if you have two files & only one column if you want to compare then..

step 1:

paste -d, file1 file2 > outputfile

step 2:

awk -F, '{if($1 == $2) {print "MATCH"} else {print "NO match"}}' outputfile

Now you will get output like below:

MATCH
MATCH
MATCH
MATCH
MATCH

Thanks
Sha:b:

Sorry I am crazy busy right now. I would have liked to reply much earlier already. But as the title is mentioning I am busy like hell. Thanks for your help, I will try it out and let you guys know what happend.

Like I said, Thanks anyway and take care for now

Sebastian

Thanks again guys for your response, but the files I would like to compare have something special to it.

Like I already mentioned, both files have the same layout:

Header
Record1
Record2
...
Footer

The header is always different, so nothing to be focused on.
The records first 25 signs are always different too.
Now the important part. After sign 25 of each record I would like to start comparing with the corresponding record in the second file.

if record1.file1 after sign25 equals record1.file2 after sign25
set return value 0
else set return value 1

and so on for all remaining records.

Note: I can't do anything about the format of the file. I get it that why and I have to work with it.

Your help would be very much appreciated

Thx,

Sebastian

Please, does anybody have an idea?

I wouldn't ask if I wouldn't have tried it first myself.

Thanks,

Sebastian:confused:

I might be able to figure out a solution for my whole problem, if somebody
could just tell me how to read an input line from a certain position.

f.ex.

input line

012345678910

I would like to use that line starting from position 5 and ignore the first 5 positions.

Any Ideas?

Thanks a lot.

Sebastian

Don't bump up your question, that's against the Simple rules of the UNIX.COM forums:

Try this:

awk '{getline line < "file1"}
{if(substr($0, 26)!=substr(line, 26))print "Line " NR " mismatch"}' file2