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!

Reading multiple files at once?

Status
Not open for further replies.

erixire

Technical User
Jun 4, 2002
72
CA
Hi,

Is it possible in Awk to read multiple files at once so that awk return the concatenation of every first line, second line, third line, etc? For example, suppose file "File01" contains:
1
2
3
4

file "File02" contains:

Dog
Cat
Bird
Snake

Then, I want to ouptut
1 Dog
2 Cat
3 Bird
4 Snake

Can this be done? If not, does anybody knows another tool that can do such a trick?

Thanks
 
It is ugly and fairly hard to do in awk.

function num_elems(arra, i,x) {
for (i in arra) {
x++
}
return x
}

BEGIN {

while ((getline < &quot;file1&quot;) > 0) {
array[a++] = $1
}
close(&quot;file1&quot;)

while ((getline < &quot;file2&quot;) > 0) {
arraytwo[m++] = $2
}
close(&quot;file2&quot;)

num1 = num_elems(array)
num2 = num_elems(arraytwo)

while (x <= num1 && x <= num2) {
++x
printf &quot;%s\t%s\n&quot;, array[x],arraytwo[x]
}
}

You have issues like:
What happens if one array is large than the other?
You can just truncate the number of elements read:
num1 = (num1 > num2) ? num2 : num1
What if you want to use ascii or descriptive subscripts?

Paste can probably do what you want.
 
Try &quot;paste&quot; command in UNIX.
I was in need of such a script and paste did the work perfectly. Go to man paste for more details.

Regards
MP
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top