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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

arrays and getline

Status
Not open for further replies.

rhnaeco

Technical User
Aug 11, 2005
45
i know this is easy and i should use arrays- however despite reading about them i still don't quite understand:

i have two files one containing:
1 Tidal Prism = 1.20397e+08 m2

the other:
1 profile area = 12309.8 m2
2 profile length = 1.06403 km

all i want to do is create a file like this which will be added to later:

1.20397e+0 12309.8


i have tried:
awk '
BEGIN {
while (getline < "tp1.txt")
{prism[$5]=1}
}
{
if ($3 == "area")
print prism[$5], $5
}' tp2.txt
but just get
1.20397e+08

i have also tried:

awk '{
if ($1 == "1")
print $5
}' tp1.txt tp2.txt

which works but obviously gives the answer in a column, rather than a line

as i said- i know the array is the problem but how do i know what x and y equals in name[x]=y, using my files as an example- i have seen y=1, y=$3, x=$1,$3 -help!!!!

thanks

rhnaeco
 
Modifying your first attempt, which looked pretty good:

Code:
awk '
BEGIN {
   while (getline < "tp1.txt") { prism[$1]=$5 } 
}
   
$3 == "area" {
   print prism[$1], $5
}' tp2.txt

Basically you should be indexing the prism array using the identifier that uniquely identifies each item of data. In this case it appears to be $1, which is the line number?


Annihilannic.
 
as i said-so close!! thanks for your reply- i think i understand arrays for awk a little better- the two $ confused me a lot! it's a pity that a lot of awk webpages seem to be the same text, so if the example is not clear, google is not always a help.

thanks again
 
I hate getline-s ;)
Code:
awk '
FNR==NR { prism[$1]=$5; next }
  
$3 == "area" && $1 in prism {
   print prism[$1], $5
}' tp1.txt tp2.txt

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top