add more data to existing data in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting add more data to existing data in a file
# 1  
Old 03-23-2011
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
Code:
#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

Code:
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:

Code:
#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
# 2  
Old 03-23-2011
Try:
Code:
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

This User Gave Thanks to bartus11 For This Post:
# 3  
Old 03-23-2011
Hi bartus11,

thanks so much...it works great!! Smilie
# 4  
Old 03-23-2011
Thanks so much... it works great!! Smilie
# 5  
Old 03-23-2011
Quote:
Originally Posted by bartus11
Try:
Code:
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

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.Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Write data into existing '.xlsx' file that has multiple sheets

Hello, I have a .xlsx file with 11 worksheets and I need to insert the contents of a text file (tab delim, roughly 30 columns with 1000 rows) from Row 3 onwards on the 2nd sheet. I have my code which can insert values into existing sheet but I am unable to write/import the contents of a text... (1 Reply)
Discussion started by: nans
1 Replies

2. Shell Programming and Scripting

Write data into existing '.xlsx' file that has multiple sheets

Hello, I have a .xlsx file with 11 worksheets and I need to insert the contents of a text file (tab delim, roughly 30 columns with 1000 rows) from Row 3 onwards on the 2nd sheet. I have my code which can insert values into existing sheet but I am unable to write/import the contents of a text... (0 Replies)
Discussion started by: nans
0 Replies

3. Shell Programming and Scripting

Help with add existing file name as new data column in new output file

Input File 1 cat S1.txt MI0043 2731 miR-1 Input File 2 cat S4.txt MI006 310 CiR-1 MI057 10 CiR-24 MI750 5 CiR-24 Desired Output File 1 cat S1.txt.out MI0043 2731 miR-1 S1.txt Desired Output File 2 cat S4.txt.out MI006 310 CiR-1 S4.txt (3 Replies)
Discussion started by: perl_beginner
3 Replies

4. Shell Programming and Scripting

Recoding data in a matrix from an existing file

Hi, I was wondering if someone would be able to help with extrapolating information from a file and filling an existing matrix with that information. I have made a matrix like this (file 1): A B C D 1 2 3 4 I have another file with data like this (file 2): 1 A 1 C 3 C 4 B... (1 Reply)
Discussion started by: hubleo
1 Replies

5. Shell Programming and Scripting

Generate tabular data based on a column value from an existing data file

Hi, I have a data file with : 01/28/2012,1,1,98995 01/28/2012,1,2,7195 01/29/2012,1,1,98995 01/29/2012,1,2,7195 01/30/2012,1,1,98896 01/30/2012,1,2,7083 01/31/2012,1,1,98896 01/31/2012,1,2,7083 02/01/2012,1,1,98896 02/01/2012,1,2,7083 02/02/2012,1,1,98899 02/02/2012,1,2,7083 I... (1 Reply)
Discussion started by: himanish
1 Replies

6. Ubuntu

How to add a data column in existing file

Hi All I need to add a column on my existing data file. I know similar posts are there but none of them were meeting my requirement. My input is 1.20 3.44 4.88 5.11 4.99 3.22 1.89 3.89 2.90 Desired output 1 1.20 3.44 4.88 2 5.11 4.99 3.22 3 1.89 3.89 2.90 I will... (2 Replies)
Discussion started by: mahbub03
2 Replies

7. Shell Programming and Scripting

How to delete already existing data in a file using perl? Pls help me!!

Dear Friends, I need urgent help from u.. I have two files,file1 & file 2.. file1 have a existing data of file2.So i want to delete those existing datas from file1 (which contain the data from file1) My file1 like this rs39348 1 1045729 A G 0.1791 0.2054 0.84 ... (3 Replies)
Discussion started by: sureshraj
3 Replies

8. UNIX for Dummies Questions & Answers

Add line with data to existing file

i have a file called motors with 3 columns separated with tabs eg: car make reg i want to create a executable file that will add a line with data eg: car make reg benz s600 t35778 can you please show me how to do this? (7 Replies)
Discussion started by: fletcher
7 Replies

9. Shell Programming and Scripting

Need to add a line of data to already existing file in Unix..

Hello.. I have a text file with 100 lines of data. I need to add 1 line of data to that already existing file at the first line( beginning of the file) , so that the already existing 100 lines will start from 2 nd line.Now the file will have 101 lines of data. Help me on how to add the line... (4 Replies)
Discussion started by: charan81
4 Replies

10. UNIX for Dummies Questions & Answers

help on appending data to existing data

I need to know how to record the hostname, date/time and all of the process and send it all to one file. I know that the commands I need are hostname, date and ps but I don't know how to do them all and send them all to the same file. Please help! (1 Reply)
Discussion started by: precious51980
1 Replies
Login or Register to Ask a Question