Compare two files and write data to second file using awk

Hi Guys,

I wanted to compare a delimited file and positional file, for a particular key files and if it matches then append the positional file with some data.

Example:

Delimited File
--------------

Byer;Amy;NONE1;A5218257;E5218257
Byer;Amy;NONE1;A5218260;E5218260

Positional File
--------------

190004000153E5218257010000000980002425852013
190004000153E5218258010000003850002425852013
190004000153E5218259010000012190002425852013
190004000153E5218260010000003850002425852013
190004000153E5218261010000012190002425852013

If the fifth filed in the delimited file is found in the positional file at position 13 then we have to append the fourth field from the delimited file into the positional file, at the end of the respective record.

Expected output

190004000153E5218257010000000980002425852013A5218257
190004000153E5218258010000003850002425852013
190004000153E5218259010000012190002425852013
190004000153E5218260010000003850002425852013A5218260
190004000153E5218261010000012190002425852013

Script Used

awk 'BEGIN{FS = ";"};FNR==NR{A[$5] = $4}
FNR!=NR{if ((x = substr($0,13,8)) in A) print $0 A[x];else print A[x]}' $1 $2 > $3

The above awk command is not working properly. It is not appending the desired data in the positional file.

Please help.

Thanks in advance!!

Thanks,
Ajay

Try this:

awk -F; 'NR==FNR{a[$5]=$4; next} {s=substr($0,13,8)} s in a {$0=$0 a}1' file1 file2

Thanks For the reply Frank, it is still not working It is writing the second file as it is in the out put file. It is not appending the data in the record.

Can l you please let me know why are you adding 1 at after the append function?

awk -F; 'NR==FNR{a[$5]=$4; next} {s=substr($0,13,8)} s in a {$0=$0 a}1' file1 file2

Thanks,
Ajay

In awk, any nonzero numeric value or any nonempty string value is true.

So 1 means true and if true, the default awk operation is print record:

awk 1 filename