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

exporting shell variable to AWK

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
I would like to access to my shell variable in this script:
mydate='date | awk '{print $3 $4}''
awk '
{
print $0, $mydate

}' namefile

It does work if I put '$mydate' my variable mydate is taken like a string($mydate) and not a variable containing the date.
 
Approach:
1. man awk
2. Read FAQ
3. Read archives


mydate=`date`;
awk -v awkDate=$mydate '{print awkDate $3 $4}'
 
Don't use date because it shows a lot of strings and the AWK expression doesn´t going to work.
But it works fine with just a string.
 
Hi asqwertyyt,

Awk cannot deal with shell variables directly,
but you can pass them in as escaped vars and
awk will expand them.


mydate=`date|awk '{printf("%s %s\n", $3,$4) }'`

nawk '
{

print $0", '"$mydate"'"

}' namefile


Hope this is what you need.



flogrr
flogr@yahoo.com

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top