awk "for loop" and variables

Reply

Join Date: Jul 2005
Posts: 2
Reputation: rhnaeco is an unknown quantity at this point 
Solved Threads: 0
rhnaeco rhnaeco is offline Offline
Newbie Poster

awk "for loop" and variables

 
0
  #1
Aug 10th, 2005
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)

can anyone help?
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 1
Reputation: swellcontour is an unknown quantity at this point 
Solved Threads: 0
swellcontour swellcontour is offline Offline
Newbie Poster

Re: awk "for loop" and variables

 
0
  #2
Jan 21st, 2008
awk runs it's script once per line of input in the file. it automatically advances to the next record and feeds the input to your script. $1 is the first field of a record, and you print it once per record
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 399
Reputation: eggi will become famous soon enough eggi will become famous soon enough 
Solved Threads: 47
eggi eggi is offline Offline
Posting Whiz

Re: awk "for loop" and variables

 
0
  #3
Jan 22nd, 2008
This is true of sed, also.

Perhaps something as simple as

var=`head -1 tex 2>&1|awk '{print $1}'`

woud do it ?

, Best wishes,

Mike
Linux and Unix Tips, Tricks and Individual Advice - The Linux and Unix Menagerie!
------------------------------------------------------------------------
The greatest viral marketing idea of all time, get your copy of this Free Report now!
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 148
Reputation: ghostdog74 is on a distinguished road 
Solved Threads: 40
ghostdog74 ghostdog74 is offline Offline
Junior Poster

Re: awk "for loop" and variables

 
0
  #4
Jan 22nd, 2008
Shell Scripting Syntax (Toggle Plain Text)
  1. awk 'NR==1{ print "var="$1}' file
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC