Reading specific contents from 1 input files and appending it to another input file

Hi guys,

I am new to AWK and unix scripting. Please see below my problem and let me know if anyone you can help.

I have 2 input files (example given below)

Input file 2 is a standard file (it will not change) and we have to get the name (second column after comma) from it and append it in the Input file 1 (see output file below highlighted in blue). The content of first input file Input file 1 will keep changing though.

If we get a match with first number [before first dot (.)] then concatenate the name from second file at the end with a comma (,).

Input file 1
Aug 14 10:37 123.XYZ.106.20061023160210.in
Aug 14 12:59 135.XYZ.107.20061023160210.in
Aug 14 13:15 234.XYZ.108.20061023160210.in

Input file 2
111,ABCD
123,EFGH
124,TYUI
125,RTYUGH
135,RTYT
139,REFG
234,GHJK
345,GHJK
346,JKHG

Output file
Aug 14 10:37 123.XYZ.106.20061023160210.in, EFGH
Aug 14 12:59 135.XYZ.107.20061023160210.in, RTYT
Aug 14 13:15 234.XYZ.108.20061023160210.in, GHJK

Appreciate your responses.

awk -F",| |\\\." 'NR==FNR{a[$1]=$2;next}
$4 in a{print $0 "," a[$4]}
' file2 file1

Use nawk, gawk or /usr/xpg4/bin/awk on Solaris.

Regards

Franklin52 - Thanks for your help. One more question though, If I have another input file

Input file 3
XYZ,Desc1
VZG,Desc2

And if I would like to add more to output file by looking into input file 3, then how would I get the "XYZ" to get the description from input file 3.

Output file
Desc1,Aug 14 10:37 123.XYZ.106.20061023160210.in, EFGH
Desc1,Aug 14 12:59 135.XYZ.107.20061023160210.in, RTYT
Desc1,Aug 14 13:15 234.XYZ.108.20061023160210.in, GHJK

I tried this but no luck

awk -F",| |\\\.\." 'NR==FNR{a[$1]=$2;next}
$4 in a{print $0 "," a[$4]}
' file3 file1

Appreciate your responses.

Try this:

awk -F",| |\\\." 'NR==FNR{a[$1]=$2;next}
$5 in a{print a[$5] "," $0}' file3 file1

Regards

No it didn't work. Let me tell you what is the scenario...

I have 3 input files (example given below)

Input file 2 & 3 are a standard file (it will not change) and we have to get the name (second column after comma) from it and append it in the Input file 1 (see output file below highlighted in blue & magenta). The content of first input file Input file 1 will keep changing though.

If we get a match with first number [before first dot (.)] then concatenate the name from second file at the start with a comma (,).

If we get a match with first string [after first dot (.)] then concatenate the name from third file at the start with a comma (,).

Input file 1
123.XYZ.106.20061023160210.in
135.XYZ.107.20061023160210.in
234.XYZ.108.20061023160210.in

Input file 2
111,ABCD
123,EFGH
124,TYUI
125,RTYUGH
135,RTYT
139,REFG
234,GHJK
345,GHJK
346,JKHG

Input file 3
XYZ,Desc1
VZG,Desc2

Output file
EFGH,Desc1,123.XYZ.106.20061023160210.in
RTYT,Desc1,135.XYZ.107.20061023160210.in
GHJK,Desc1,234.XYZ.108.20061023160210.in

I got the first part as following :-
awk -F",| |\\\." 'NR==FNR{a[$1]=$2;next}
$1 in a{print a[$1] "," $0}
' file2 file1 >> output file

But I am not getting the second part.

Appreciate you response.

Try this:

awk -F",|\\\." '
FILENAME=="file3" {a[$1]=$2;next}
FILENAME=="file2" {b[$1]=$2;next}
a[$2] && b[$1] {$0=b[$1]"," a[$2]"," $0}
{print}' file3 file2 file1

Regards