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 and using multiple variables 1

Status
Not open for further replies.

kimmers

Technical User
Aug 1, 2002
24
FI
Hello,

I have one small question where you awk-users can propably give an answer

I have a text file (file.txt) containing two columns and multiple rows
#cat file.txt
Alfa 0001
Beta 0002
Gamma 0003
.
..
...

In my program I do the following for each line using a program i.e which converts Fahrenheits to Celcius

for i in `awk '{print $2}' file.txt`
do
program_name $i >> output.txt
#here for each line we feed the second column as a parameter
done

What I would like to do is to get as a result
cat output.txt
In temperature Alfa converted from Fahrenheit to Celcius is X degrees
In temperature Beta converted from Fahrenheit to Celcius is X degrees
In temperature Gamma converted from Fahrenheit to Celcius is X degrees

So
The principal using variables doesn't exactly work as I hope there is a slight glitch where I need your assistance

for i in `awk '{a=($1),b=($2),print a,b}' file.txt`

do
echo "In temperature" $a "converted from Fahrenheit to Celcius is" program_name $b "degrees" >> output.txt
done

Kind Regards,

Kimmers,
hd_starsky@yahoo.com
 
Try this:

for i in `awk '{print $1,$2}' file.txt`

do
a = `echo $i | awk '{print $1}'
b = `echo $i | awk '{print $2}'
echo "In temperature" $a "converted from Fahrenheit to Celcius is" program_name $b "degrees" >> output.txt
done

Talent is what you possess;
genius is what possesses you

 
Let awk build the command lines to execute, then read these command lines and evaluate them
Code:
awk '
{
  printf("echo \"In temperature %s converted from Fahrenheit to Celcius is `program_name %s` degrees\"\n",$1,$2);
}' file.txt | while read line; do eval $line; done
 
Hi,

And thank You very much for your responses!

I tested them out and Shantha, if you don't mind can you check the operability of your script.
Or it might be my ksh environnment in IBM AIX which is causing it.

I get the following error:
test[14]: a: not found.
test[14]: b: not found.

Where my program_name is test. Seems like it is missing the definition of those "variables" a and b.
Is it recommended to initialize the variable like in C?
I also tested adding the hyphens at the end but that wasn't the case
a = `echo $i | awk '{print $1}'` <--
b = `echo $i | awk '{print $2}'` <--


Dchoulette, yours worked fine it was more C-like representation.

Regards,

#Kimmers
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top