- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- awk print variable
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-01-2004 11:20 PM
03-01-2004 11:20 PM
awk print variable
I need to something like the following:
2 variables eg
sedate=20040302
setime=130000
echo a string to create a new one:
echo "29Feb 14:21:15, ,XXXXXXXX,FOS00001980,P63566,0293/108458,1/9,Not extracted" |awk -F, '{print $1","$sedate $setime","$3","$4","$5","$6","$7}'
However I have not got the handling of the variables right, as I get :
awk: Field $() is not correct.
The input line number is 1.
The source line number is 1.
Any help apreciated!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-01-2004 11:26 PM
03-01-2004 11:26 PM
Re: awk print variable
printf "%s,%s %s,%s,%s,%s,%s,%s",$1,$sedate,$setime,$3,$4,$5,$6,$7
INstead of your "print" statement
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-01-2004 11:28 PM
03-01-2004 11:28 PM
Re: awk print variable
awk -v var1=$setdate -v var2=$setime '{
print $1","var1","var2 ...
Regards,
Jean-Luc
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-02-2004 01:57 AM
03-02-2004 01:57 AM
Re: awk print variable
You need to get those variables into awk. Jean-Luc shows a good solution to that using command line arguments.
An alternative is to use the ENVIRON array that awk provides. Something like (untested):
awk -F, 'print $1,ENVIRON["sedate"] ENVIRON["setime"],$3 ....
hth,
Hein.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-02-2004 03:39 AM
03-02-2004 03:39 AM
Re: awk print variable
here are a couple of ways without using awk
using sed. be sure to use double quotes
print "29Feb 14:21:15, ,XXXXXXXX,FOS00001980" | sed "s/\(.*\),,/\1,$sedate $setime,/"
and just using the shell
string="29Feb 14:21:15, ,XXXXXXXX,FOS00001980"
last=${string#*,}
first=${string%%,*}
printf "%s,%s %s%s\n" $first $sedate setime $last
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-03-2004 06:11 AM
03-03-2004 06:11 AM
Re: awk print variable
SETDATE=20040302
SETTIME=140000
echo "29Feb 14:21:15,,XXXXXXXX,FOS00001980,P63566,0293/108458,1/9,Not extracted" |awk '{print $1","SEDATE" "SETIME" ,"$3","$4","$5","$6","$7}' SEDATE=${SETDATE} SETTIME=${SETTIME}
or
echo "29Feb 14:21:15,,XXXXXXXX,FOS00001980,P63566,0293/108458,1/9,Not extracted" |awk '{print $1","SEDATE" "SETIME" ,"$3","$4","$5","$6","$7}' SEDATE=20040302 SETTIME=140000
Rory
(I tested #2 not #1 but both should work)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-03-2004 08:02 AM
03-03-2004 08:02 AM
Re: awk print variable
Why don't you just echo also the variables into the pipeline ? Something like:
sedate=20040302
setime=130000
echo a string to create a new one:
echo "29Feb 14:21:15,$setdate,$setime,XXXXXXXX,FOS00001980,P63566,0293/108458,1/9,Not extracted" |awk -F, '{print $1","$2","$3","$4","$5","$6","$7","$8}'
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-03-2004 10:28 AM
03-03-2004 10:28 AM
Re: awk print variable
echo "SEDATE",$sedate > myfile
echo "SETIME",$setime >> myfile
echo "29Feb 14:21:15, ,XXXXXXXX,FOS00001980,P63566,0293/108458,1/9,Not extracted" >> myfile
NOTE you could actually echo multiple of these last lines and have things work if you had a constant SEDATE and SETIME that you wanted to use and multiple "data" lines.
Then create an awk file called se.awk
/^SEDATE/ {sedate=$2;next;}
/^SETIME/ {setime=$2;next;}
{print $1","sedate setime","$3","$4","$5","$6","$7}
Then run:
awk -f se.awk myfile > myoutputfile
Best regards,
Kent M. Ostby