Reading and appending a row from file1 to file2 using awk or sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Reading and appending a row from file1 to file2 using awk or sed
# 1  
Old 05-16-2014
Reading and appending a row from file1 to file2 using awk or sed

Hi, I wanted to add each row of file2.txt to entire length of file1.txt given the sample data below and save it as new file. Any idea how to efficiently do it. Thank you for any help.

Code:
input file
file1.txt   file2.txt
140 30    200006 141 32     
140 32    200006 142 33
140 35    200006 142 30
140 36
140 50

desired outputs:
outfile1.txt
140 30 200006 141 32
140 32 200006 141 32
140 35 200006 141 32
140 36 200006 141 32
140 50 200006 141 32

outfile2.txt
140 30 200006 142 33
140 32 200006 142 33
140 35 200006 142 33
140 36 200006 142 33
140 50 200006 142 33

outfile3.txt
140 30 200006 142 30
140 32 200006 142 30
140 35 200006 142 30
140 36 200006 142 30
140 50 200006 142 30

# 2  
Old 05-16-2014
Code:
awk 'NR == FNR {a[FNR] = $0; n = FNR; next}
{c++;
for(i = 1; i <= n; i++)
  {print a[i], $0 > "output" c ".txt"}}' file1.txt file2.txt

This User Gave Thanks to SriniShoo For This Post:
# 3  
Old 05-16-2014
Thank you for the quick reply and saving my day. Smilie
# 4  
Old 05-16-2014
Quote:
Originally Posted by SriniShoo
Code:
awk 'NR == FNR {a[FNR] = $0; n = FNR; next}
{c++;
for(i = 1; i <= n; i++)
  {print a[i], $0 > "output" c ".txt"}}' file1.txt file2.txt

This will work fine on many systems as long as there aren't too many lines in file2.txt. On other systems, you'll get a syntax error on the print statement. If there are a lot of lines in file2.txt, it may run out of file descriptors. The following minor changes avoid these possible problems:
Code:
awk '
NR == FNR {a[FNR] = $0; n = FNR; next}
{       c++;
        for(i = 1; i <= n; i++)
                print a[i], $0 > ("output" c ".txt")
        close("output" c ".txt")
}' file1.txt file2.txt

This User Gave Thanks to Don Cragun For This Post:
# 5  
Old 05-16-2014
Thank you for pointing that out. In the output file, how can I modify "c" to have the same length by adding leading zeros, such that output1.txt should be output01.txt?thanks again
# 6  
Old 05-16-2014
Quote:
Originally Posted by ida1215
Thank you for pointing that out. In the output file, how can I modify "c" to have the same length by adding leading zeros, such that output1.txt should be output01.txt?thanks again
Code:
awk '
NR == FNR {a[FNR] = $0; n = FNR; next}
{       f = sprintf("output%02d.txt", ++c)
        for(i = 1; i <= n; i++)
                print a[i], $0 > f
        close(f)
}' file1.txt file2.txt

This User Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Keep only the closet match of timestamped row (include headers) from file1 to precede file2 row/s

This is a question that is related to one I had last August when I was trying to sort/merge two files by millsecond time column (in this case column 6). The script (below) that helped me last august by RudiC solved the puzzle of sorting/merging two files by time, except it gets lost when the... (0 Replies)
Discussion started by: aachave1
0 Replies

2. Shell Programming and Scripting

awk to update field in file2 if not the same as file1

Trying to use awk to: update $2 in file2 with the $2 value in file1, if $1 in file1 matches $13 in file2, which is tab-delimeted. The $2values may already be the same so in that case nothing happens and the next line is processed. There are exactly 4,605 unique $13 values. Thank you :). ... (4 Replies)
Discussion started by: cmccabe
4 Replies

3. Shell Programming and Scripting

awk to search field2 in file2 using range of fields file1 and using match to another field in file1

I am trying to use awk to find all the $2 values in file2 which is ~30MB and tab-delimited, that are between $2 and $3 in file1 which is ~2GB and tab-delimited. I have just found out that I need to use $1 and $2 and $3 from file1 and $1 and $2of file2 must match $1 of file1 and be in the range... (6 Replies)
Discussion started by: cmccabe
6 Replies

4. UNIX for Beginners Questions & Answers

Keep only the closet match of timestamped row (include headers) from file1 to precede file2 row/s

My original files are like this below and I distinguish them from the AP_ID (file1 has 572 and file2 has 544). Also, the header on file1 has “G_” pre-pended. NOTE: these are only snippets of very large files and much of the data is not present here. Original File 1: ... (36 Replies)
Discussion started by: aachave1
36 Replies

5. Shell Programming and Scripting

Get row number from file1 and print that row of file2

Hi. How can we print those rows of file2 which are mentioned in file1. first character of file1 is a row number.. for eg file1 1:abc 3:ghi 6:pqr file2 a abc b def c ghi d jkl e mno f pqr ... (6 Replies)
Discussion started by: Abhiraj Singh
6 Replies

6. UNIX for Dummies Questions & Answers

if matching strings in file1 and file2, add column from file1 to file2

I have very limited coding skills but I'm wondering if someone could help me with this. There are many threads about matching strings in two files, but I have no idea how to add a column from one file to another based on a matching string. I'm looking to match column1 in file1 to the number... (3 Replies)
Discussion started by: pathunkathunk
3 Replies

7. Shell Programming and Scripting

[Solved] delete line from file1 by reading from file2

Hi All, I have to arrange one of the text file by deleting specific lines. cat file1.txt 3595 3595 -0.00842773 -0.0085077 0.00368851 12815 12815 -0.00929239 0.00439785 0.0291697 3747 3747 -0.00974353 0.00228922 0.0225058 3574 3574 -0.00711399 -0.00315748 0.0141206 .... 12734... (7 Replies)
Discussion started by: senayasma
7 Replies

8. Shell Programming and Scripting

awk/sed search lines in file1 matching columns in file2

Hi All, as you can see I'm pretty new to this board. :D I'm struggling around with small script to search a few fields in another file. Basically I have file1 looking like this: 15:38:28 sz:10001 pr:14.16 15:38:28 sz:10002 pr:18.41 15:38:29 sz:10003 pr:19.28 15:38:30 sz:10004... (1 Reply)
Discussion started by: floripoint
1 Replies

9. Shell Programming and Scripting

Awk Compare File1 File2 on f2

I'm trying to compare two files using AWK, where if field2 of both files match, replace field1 of file1 with field1 of file2 and if there is no match just print the line of file1. file1.txt (has empty first field) :ABBATOM:B:H:1992 :ABBA TROJAN:B:H:1993 :ABBES FIRST HOPE:B:M:1997 :ABBEYS... (4 Replies)
Discussion started by: RacerX
4 Replies

10. Shell Programming and Scripting

Awk Compare f1,f2,f3 of File1 with f1 of File2

I have an Awk string-compare problem and have searched the internet and forums for a solution i could use but cannot find a solution i understand to make work with my particular problem: I need to compare (field1 field2 field3 of File1) against (field1 of File2) and if they match print out... (6 Replies)
Discussion started by: RacerX
6 Replies
Login or Register to Ask a Question