- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- How to get output of a shell command within awk va...
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
Discussions
Discussions
Forums
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
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
тАО10-07-2009 07:13 AM
тАО10-07-2009 07:13 AM
How to get output of a shell command within awk variable.
awk '{
dt="\"Oct 7 15:06:18\""
x="'"`date +%s -d $dt`"'"
printf "%s\n",x
}'
It is not working properly but I need similar solution. I would like to pass the dt varible to evaluate the epoch time and get it within the variable x.
- Tags:
- awk
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-07-2009 08:08 AM
тАО10-07-2009 08:08 AM
Re: How to get output of a shell command within awk variable.
In HP-UX or HP's 'awk' this isn't going to work. You can, however, accomplish this easily in Perl:
# perl -MTime::Local -le 'print +(timelocal(18,6,15,7,9,109))'
1254942378
# perl -le 'print scalar localtime(1254942378)'
Wed Oct 7 15:06:18 2009
The arguments to pass to timelocal() are:
seconds, minutes, hours, month_day, month, year
...in that order where:
The 'month' is the calendar month number less one (1) and the year has 1900 subtracted from it. That is, months are zer0-relative.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-07-2009 08:11 AM
тАО10-07-2009 08:11 AM
Re: How to get output of a shell command within awk variable.
"command" | getline
something along the lines of this should be close
awk '{
dt="\"Oct 7 15:06:18\""
"'"`date +%s -d $dt`"'" | getline x
printf "%s\n",x
}'
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-07-2009 08:15 AM
тАО10-07-2009 08:15 AM
Re: How to get output of a shell command within awk variable.
and, the way you've got it, it would do it once per input line being processed,
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-07-2009 10:28 AM
тАО10-07-2009 10:28 AM
Re: How to get output of a shell command within awk variable.
In a GNU environment, this works:
# cat ./epoch.awk
BEGIN{
print "Evaluating: " dt
"date +%s -d" "\"" dt "\"" | getline x
printf "%s\n",x
}
...run as:
# awk -v dt="Oct 7 15:06:18" -f ./epoch.awk
Evaluating: Oct 7 15:06:18
1254942378
# awk -v dt="Sep 8 21:46:39 2001" -f ./epoch.awk
Evaluating: Sep 8 21:46:39 2001
999999999
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-07-2009 03:17 PM
тАО10-07-2009 03:17 PM
Re: How to get output of a shell command within awk variable.
Do you care that the date is evaluated outside before you run awk?
dt="Oct 7 15:06:18"
x=$(date +%s -d "$dt")
awk -v x="$x" 'BEGIN { print x }' /dev/null
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-07-2009 04:18 PM
тАО10-07-2009 04:18 PM
Re: How to get output of a shell command within awk variable.
var=$(awk '{
dt="\"Oct 7 15:06:18\""
"'"`date +%s -d $dt`"'" | getline x
printf "%s\n",x
}')
This will put the output of the command in brackets int he variable var
The command itself needs some work.
SEP
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-07-2009 10:38 PM
тАО10-07-2009 10:38 PM
Re: How to get output of a shell command within awk variable.
I would like to filter syslog based on the timestamp. If I process syslog using awk, then the combination of $1,$2,$3 provide the timestamp. So I would like to create the timestamp for each record as,
dt="\""$1" "$2" "$3"\""
Next I would like to convert it to epoch time as,
x="'"`date +%s -d $dt`"'"
Next I would like to take decision based on the value of x.
But the problem is that since I am escaping to shell the awk variable is not available. So how can I pass it to shell. Is it possible to define dt as shell variable and dynamically assign the value from awk ?
Thanks
Sourav
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-08-2009 04:06 AM
тАО10-08-2009 04:06 AM
Re: How to get output of a shell command within awk variable.
Thanks everybody for your reply, but my objective is bit different.
> You need to be more clear in your questions, then, at the onset.
> ...Next I would like to convert it to epoch time...
I showed you two ways to do that, using Perl and using a GNU 'date' command which offers the '-d' option. HP-UX's 'date' does not have that ability.
You can see a Perl solution in my post above on Oct 7, 2009 16:08:50 . Using GNU's 'date' I showed you how your script could work by passing a string in my post dated Oct 7, 2009 18:28:57, above.
>...since I am escaping to shell the awk variable is not available. So how can I pass it to shell. Is it possible to define dt as shell variable and dynamically assign the value from awk ?
OK, quite simply you can use this script:
# cat ./epoch.awk
BEGIN{
"date +%s -d" "\"" dt "\"" | getline x
printf "%s\n",x
}
You can use it any of these ways:
# EPOCH=$(awk -v dt="Sep 8 21:46:39 2001" -f ./epoch.awk)
# echo ${EPOCH}
999999999
(or)
# dt="Sep 8 21:46:39 2001"
# EPOCH=$(awk -v dt="${dt}" -f ./epoch.awk)
# echo ${EPOCH}
999999999
Thus your shell script can pass a shell variable to 'awk' and receive the value of a shell variable from 'awk'. Be sure to double quote your 'dt' variable when you pass it to 'awk' since you need to preserve the whitespaces it contains!
Regards!
...JRF...