add more data to existing data in a file

Hi all,

I need help to add additional data from file2 to existing data in file 1 using awk, sed or perl. the ID in file 1 should match against field $3 in file2

file1

#this is a new game
ID      HR_1
BASE1   30
BASE2   37
DETAIL  No
TYPE    L
@@
ID      HR_10
BASE1   6030
BASE2   8601
DETAIL  Available
TYPE    L
@@
ID      HR_4
BASE1   4100
BASE2   4500
DETAIL  Available
TYPE    F
@@

file2

Val    142-143    HR_1
Val    154-160    HR_1
Val    199-205    HR_4
Val    150-193    HR_4
Val    237-366    HR_4
Val    644-699    HR_4
Val    722-728    HR_10
Val    862-846    HR_10
Val    695-855    HR_50

The output should show like this:

#this is a new game
ID      HR_1
BASE1   30
BASE2   37
DETAIL  No
TYPE    L
Val     142-143 
Val     154-160
@@
ID       HR_10
BASE1    6030
BASE2    8601
DETAIL   available
TYPE     L
Val      722-728    
Val      862-846    
@@
ID        HR_4
BASE1     4100
BASE2     4500
DETAIL    available
TYPE      F
Val       199-205    
Val       150-193    
Val       237-366    
Val       644-699    
@@

i tried to do it with some awk codes but the output that i got totally messed up. Please help me on this. Thanks

Try:

awk 'NR==FNR{a[$3]=a[$3]"\n"$1"\t"$2;next}/ID/{x=$2}/TYPE/{sub("^\n","",a[x]);$0=$0"\n"a[x]}1' file2 file1
1 Like

Hi bartus11,

thanks so much...it works great!! :smiley:

Thanks so much... it works great!! :smiley:

Hey man, if would do this problem, I would just make a new file and append each line of both files and make an if statement. That is because I'm just a beginner. But I want to learn more about the "awk" command. Is it okay if you explain each parameter in the code that you just made? I only understood some of them a bit.

Thanks.:slight_smile: