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!

Sore awk output in variable

Status
Not open for further replies.

mxstein

Vendor
May 17, 2005
7
US
I would like to store the output of an awk command into a variable to be used later.

I have a file called "names"

This file has the data below
first last cuid

awk '{print $2 ; }' names
will list "last"

I'd like to store that in a variable called last like below but it doesn't work

last='awk '{print $2 ; }' names'

I'd appreciate some help. I think I have to do something with the quotes to get the output of the awk to go into the variable last, but I don't know how.
 
In your shell man page take a look at 'command substitution':
Either:
last=$(awk '{print $2}' names)
Or:
last=`awk '{print $2}' names`

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Hi PHV,

This worked but what happens if I have a file that has multiple lines, each of those have a few filed. Here's example file:

db1 user1 pass1
db2 user2 pass2
db3 user3 pass3

this file is called db_definition.

I need to do, for each record, declare DB=$1, USER_NAME=$2, PSW=$3 and then call a function that will use these 3 variables.

I tried this:
awk '{
for (i=1 ; i<=NR; i++)
{
"DB"=$1;
"USR_NAME"=$2;
"PSW"=$3;
check_db;
}
}' db_definition

where check_db is the function that take in those three variables.

Your help is much appreciated!!

Annie
 
check_db is the function
An awk or a shell function ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top