Merging data from 2 files of different lengths?

Hi all,

Sorry if someone has answered something like this already, but I have a problem. I am not brilliant with "awk" but think it should be the command to use to get what I am after.

I have 2 files:

job-file (several hundred lines like):

1018003,LONG MU WAN,1113S
1018004,LONG MU WAN,1113S
1018024,HS WAGNER,1133S
1018026,ANL WARRAIN,032S
1018032,TIAN XING HE,018S
1018055,TIAN XING HE,018S
1018057,LONG MAN BILL,918S

Vessel File (several hundred lines like):

TIAN XING HE,018S,110904,1400
ANL WARRAIN,032S,110909,0635
HS WAGNER,1133S,110905,2000
LONG MU WAN,1113S,110908,1400
SANTA MONICA,004S,110819,0630
HANJIN PORT ADELAIDE,785,110901,0630
ATHENA,134S,110905,1000
CSCL SANTIAGO,0050S,110829,1100
JPO VELA,132S,110831,1430
DIAMANTIS P,783,110823,1430
ANL BINBURRA,239S,110823,1430
MSC NORA,63A,110821,0612
STX CORINTHIAKOS,0014E,110904,0630

I want an output to look like:

1018003,LONG MU WAN,1113S,110908,1400
1018004,LONG MU WAN,1113S,110908,1400
1018024,HS WAGNER,1133S,110905,2000
1018026,ANL WARRAIN,032S,110909,0635
1018032,TIAN XING HE,018S,110904,1400
1018055,TIAN XING HE,018S,110904,1400

Basically, take all 3 fields from job-file and append fields 3 and 4 from vessel-file when fields 2 and 3 in job file = fields 1 and 2 (respectively) in vessel file. If these fields don't match there is to be no output for that entry in either file.

And quite frankly I am stumped...

I am working in Korn Shell...

Any help would be hugely appreciated.

Thanks

Steve

Hi,

just go through the below link,

Cheers,

Ranga:)

Hi,

Thanks for that - I have tried going through the example - I can get the example to do what it advertises, but cannot work out how to apply that to my files...

Thanks

Steve

Try...

$ head vessel_file job_file
==> vessel_file <==
TIAN XING HE,018S,110904,1400
ANL WARRAIN,032S,110909,0635
HS WAGNER,1133S,110905,2000
LONG MU WAN,1113S,110908,1400
SANTA MONICA,004S,110819,0630
HANJIN PORT ADELAIDE,785,110901,0630
ATHENA,134S,110905,1000
CSCL SANTIAGO,0050S,110829,1100
JPO VELA,132S,110831,1430
DIAMANTIS P,783,110823,1430

==> job_file <==
1018003,LONG MU WAN,1113S
1018004,LONG MU WAN,1113S
1018024,HS WAGNER,1133S
1018026,ANL WARRAIN,032S
1018032,TIAN XING HE,018S
1018055,TIAN XING HE,018S
1018057,LONG MAN BILL,918S

$ awk 'BEGIN{FS=OFS=","}NR==FNR{a[$1]=$3 FS $4;next}a[$2]{print $0,a[$2]}' vessel_file job_file > output_file

$ cat output_file
1018003,LONG MU WAN,1113S,110908,1400
1018004,LONG MU WAN,1113S,110908,1400
1018024,HS WAGNER,1133S,110905,2000
1018026,ANL WARRAIN,032S,110909,0635
1018032,TIAN XING HE,018S,110904,1400
1018055,TIAN XING HE,018S,110904,1400
1 Like

Thank you so much for that - it's perfect... Now I just need to work out how it works :slight_smile:

Much appreciated Ygor...