For Programmers: Free Programming Magazines  


Home > Archive > AWK > November 2004 > Command substitution/backtick problem









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Command substitution/backtick problem
tylernt

2004-11-16, 6:50 pm

I want to feed awk a line, and have it spit out a reformatted line --
converting a UNIX epoch date to human-readable format. So, input to
awk would be

one two 1082567969 four five

and the output from awk would be

one two Wed Apr 21 18:19:29 MDT 2004 four five

So, the command that I would like to run in bash (ksh is not
available) is:

tail -f filename | awk '{print $1, $2, `date -d "1970-01-01 $3 sec"`,
$4, $5}'

But, obviously, this does not work. I have tried all sorts of
combinations of backticks and backslashes, quotes, double quotes,
tried the system() command, everything. But I can't figure it out.

Can anyone help me? Even if the solution uses something other than awk
or date -d?

Thanks!!
Ed Morton

2004-11-16, 6:50 pm



tylernt wrote:
> I want to feed awk a line, and have it spit out a reformatted line --
> converting a UNIX epoch date to human-readable format. So, input to
> awk would be
>
> one two 1082567969 four five
>
> and the output from awk would be
>
> one two Wed Apr 21 18:19:29 MDT 2004 four five
>
> So, the command that I would like to run in bash (ksh is not
> available) is:
>
> tail -f filename | awk '{print $1, $2, `date -d "1970-01-01 $3 sec"`,
> $4, $5}'
>
> But, obviously, this does not work. I have tried all sorts of
> combinations of backticks and backslashes, quotes, double quotes,
> tried the system() command, everything. But I can't figure it out.
>
> Can anyone help me? Even if the solution uses something other than awk
> or date -d?


Use "system()" to call external commands. For this particular job,
you're probably better off sticking with a shell script than wrapping it
in awk.

Ed.


> Thanks!!

Ulrich M. Schwarz

2004-11-16, 6:50 pm

tylernt@gmail.com (tylernt) writes:

> I want to feed awk a line, and have it spit out a reformatted line --
> converting a UNIX epoch date to human-readable format. So, input to
> awk would be
>
> one two 1082567969 four five
>
> and the output from awk would be
>
> one two Wed Apr 21 18:19:29 MDT 2004 four five


echo "o t 1082567969 f f" | awk '$3=strftime("%a %b %d %H:%M:%S %Z %Y", $3)'

But I'm sure Kenny will shave off another dozen chars off that ;-)

Ulrich
--
"I am now forced to postulate a Heraldic-Taste-based anti-spam
blocklist for top-level-domains. I can just imagine the SMTP
reject codes... 550: go away - your flag looks like a fish."
-- Tanuki
John W. Krahn

2004-11-16, 6:50 pm

tylernt wrote:
> I want to feed awk a line, and have it spit out a reformatted line --
> converting a UNIX epoch date to human-readable format. So, input to
> awk would be
>
> one two 1082567969 four five
>
> and the output from awk would be
>
> one two Wed Apr 21 18:19:29 MDT 2004 four five
>
> So, the command that I would like to run in bash (ksh is not
> available) is:
>
> tail -f filename | awk '{print $1, $2, `date -d "1970-01-01 $3 sec"`,
> $4, $5}'
>
> But, obviously, this does not work. I have tried all sorts of
> combinations of backticks and backslashes, quotes, double quotes,
> tried the system() command, everything. But I can't figure it out.
>
> Can anyone help me? Even if the solution uses something other than awk
> or date -d?


$ echo one two 1082567969 four five | perl -pe's/(\b\d{8,10}\b)/localtime $1/ge'
one two Wed Apr 21 10:19:29 2004 four five



John
--
use Perl;
program
fulfillment
William Park

2004-11-16, 6:50 pm

In <comp.unix.shell> tylernt <tylernt@gmail.com> wrote:
> I want to feed awk a line, and have it spit out a reformatted line --
> converting a UNIX epoch date to human-readable format. So, input to
> awk would be
>
> one two 1082567969 four five
>
> and the output from awk would be
>
> one two Wed Apr 21 18:19:29 MDT 2004 four five
>
> So, the command that I would like to run in bash (ksh is not
> available) is:
>
> tail -f filename | awk '{print $1, $2, `date -d "1970-01-01 $3 sec"`,
> $4, $5}'
>
> But, obviously, this does not work. I have tried all sorts of
> combinations of backticks and backslashes, quotes, double quotes,
> tried the system() command, everything. But I can't figure it out.
>
> Can anyone help me? Even if the solution uses something other than awk
> or date -d?


Why Awk? Shell is just fine.
tail -f filename | while read a b sec c d; do
echo $a $b `date -d "1970-01-01 $sec sec"` $c $d
done

--
William Park <opengeometry@yahoo.ca>
Open Geometry Consulting, Toronto, Canada
javier

2004-11-17, 8:55 pm

Why not? It happens to be the simplest solution for everybody (unless you
have a simpler one in awk)
BTW nobody said awk is off the topic.


"Kenny McCormack" <gazelle@yin.interaccess.com> wrote in message
news:cmo88n$5gf$1@yin.interaccess.com...
> In article <2v2sfqF2hpo1qU1@uni-berlin.de>,
> William Park <opengeometry@yahoo.ca> wrote:
>
> Why shell? AWK is on-topic here.
>



javier

2004-11-21, 3:57 am

Why not? It happens to be the simplest solution for everybody (unless you
have a simpler one in awk)
BTW nobody said awk is off the topic.


"Kenny McCormack" <gazelle@yin.interaccess.com> wrote in message
news:cmo88n$5gf$1@yin.interaccess.com...
> In article <2v2sfqF2hpo1qU1@uni-berlin.de>,
> William Park <opengeometry@yahoo.ca> wrote:
>
> Why shell? AWK is on-topic here.
>



Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com