Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Read a file and print a few records 1

Status
Not open for further replies.

learningawk

Technical User
Oct 15, 2002
36
US
I have a file1:

aaa aaaa aa aaaa a
bbb bbbb bb bbbb b
ccc cccc cc cccc c
ddd dddd dd dddd d
eee eeee ee eeee e
fff ffff ff ffff f
ggg gggg gg gggg g
hhh hhhh hh hhhh h

I have a file2:
999.00000 999.00000 999.00000 2
999.00000 999.00000 999.00000 4
999.00000 999.00000 999.00000 7
etc.

I want to getline the value from $4 from file2 and insert the record " 999.00000 999.00000 999.00000" into file1. The field $4 is NR position of the 999.0000 record located in the output fille.

Thus the output would be:
aaa aaaa aa aaaa a
999.00000 999.00000 999.00000
bbb bbbb bb bbbb b
999.00000 999.00000 999.00000
ccc cccc cc cccc c
ddd dddd dd dddd d
999.00000 999.00000 999.00000
eee eeee ee eeee e
fff ffff ff ffff f
ggg gggg gg gggg g
hhh hhhh hh hhhh h

I'm not sure if I have to getline file2 into an array and then increment the printing of file1 or if you can just read file 2 and print required number from file 1 until end of both files encountered.

Thanks for any help.
 
This is untested

BEGIN {
while ((getline < &quot;file2&quot;) > 0) {
ix = $4
$4 = &quot;&quot;
a[ix] = $0
}
}
{
print
j++
if (a[j]) print a[j++]
} CaKiwi

&quot;I love mankind, it's people I can't stand&quot; - Linus Van Pelt
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top