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!

awk for loops and variables 2

Status
Not open for further replies.

rhnaeco

Technical User
Aug 11, 2005
45
hi, i have a series of files like this one:

D22 L58 two
O12 L58 two
Z5 L58 two
Z19 L58 two

and i want to make the first column into a variable ($var) that will be used in the rest of the shell script
This is my awk script so far :
awk '{
for (i = 1; i <= 1; i++)
print "var="$i
}' L58vector.txt > test.txt


but instead of producing a file (test.txt) containing just one ' var=D22'
i get:
var=D22
var=O12
var=Z5
var=Z19
which renders the variable useless.

i then want to match this variable to the same number in another file which contains latitude and longitude data and print them to an output file before starting again with the next variable (var=O12)
 
Why not doing all the job inside awk ?
What is the layout of the file which contains latitude and longitude data ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
i want it so that, for the first run of the loop, a variable is produced from input.dat e.g var=D22. Then i want the script to find this variable in the first column of my coords.dat:

... ........ ........
Z14 12.63282 45.98633
D22 12.45367 45.65229
O12 12.73299 45.67917
... ........ ........

and print out the whole line in out.dat.
After, i want the script to back to the beginning and change the varable so it becomes the second $1 in input.dat i.e var=O12, the appropriate line is then found again in coords.dat (i have about 12 variables in the each input.dat file to about 300 possible coordinates) and prints the $0 to >> out.dat

out.dat should then look like:

D22 12.45367 45.65229
O12 12.73299 45.67917
etc.

hope this makes more sense, thanks for your answers so far
 
I had tried using cut for this, but i couldn't make it work before - but this works perfectly! thank you
 
And as we are in the awk forum, here an awk solution:
awk 'NR==FNR{a[$1];next}($1 in a)' L58vector.txt coords.dat > out.dat

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
This is a very common and simple problem.
Two input files are given on the command line:
[tt]awk -f prog.awk L58vector.txt coords.dat > out.dat[/tt]

A breakdown of the solution:

Check whether we're reading lines from the first file.
A more tricky way to check is to say [tt]NR==FNR[/tt].

Code:
ARGV[1]==FILENAME {

If so store first field in associative array.

Code:
  a[$1]

Some people would say [tt]a[$1]++[/tt], but that is unnecessary. Whenever you reference an entry that doesn't exist, it is created.

Now read next line and skip to top of program.

Code:
  next
}

If this part of the code is being executed, we're reading the second file.

If field 1 was stored in the array (i.e., it was found in the first file), print the line.

Code:
$1 in a
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top