Operating System - HP-UX
1753877 Members
7502 Online
108809 Solutions
New Discussion юеВ

How to get output of a shell command within awk variable.

 
Sourav Basak
New Member

How to get output of a shell command within awk variable.

Please check the piece of code below,

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.
8 REPLIES 8
James R. Ferguson
Acclaimed Contributor

Re: How to get output of a shell command within awk variable.

Hi:

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...

OldSchool
Honored Contributor

Re: How to get output of a shell command within awk variable.

use the form

"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
}'


OldSchool
Honored Contributor

Re: How to get output of a shell command within awk variable.

fyi...my original only works if the date command process the options the way you want it.

and, the way you've got it, it would do it once per input line being processed,
James R. Ferguson
Acclaimed Contributor

Re: How to get output of a shell command within awk variable.

HI (again):

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...

Dennis Handly
Acclaimed Contributor

Re: How to get output of a shell command within awk variable.

This doesn't look like HP-UX code, no -d for date(1).

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
Steven E. Protter
Exalted Contributor

Re: How to get output of a shell command within awk variable.

Shalom,

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
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Sourav Basak
New Member

Re: How to get output of a shell command within awk variable.

Thanks everybody for your reply, but my objective is bit different.

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
James R. Ferguson
Acclaimed Contributor

Re: How to get output of a shell command within awk variable.

Hi (again):

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...