Reputation: 635
I have input in below format
0001580-160219044548744-oozie-oozi-W@:start: - - - - OK :start: 0 - :START: 2016-02-24 13:34:46 GMT OK 2016-02-24 13:34:46 GMT
0001580-160219044548744-oozie-oozi-W@PrepareHDFS - - - - OK PrepareHDFS 0 - fs 2016-02-24 13:34:46 GMT OK 2016-02-24 13:34:47 GMT
Using awk I am trying to print the required columns positioned in 7,12,16 Columns as below using the below command
cat input | awk '/^000/ {printf "%-40s %-10s %-10s",$7,$12,$16}'
and the output is
:start: 05:35:56 05:35:56
PrepareHDFS 05:35:56 05:35:57
My requirement is along with the above output I need time difference also. I tried below one inside a script
cat intput | awk '/^000/ {printf "%-40s %-10s %-10s",$7,$12,$16;
T1=`date +%s -d $12`;T2=`date +%s -d $16`;
DIFF=`expr ${SEC2} - ${SEC1}`; print `date +%H:%M:%S -ud ${DIFF}` }'
But I am getting errors as invalid syntax. How can I achieve the time difference so that output should be
PrepareHDFS 05:35:56 05:35:57 00:00:01
ScheduleStart 05:35:57 05:36:11 00:00:14
EDIT:
For time difference, I have below script
TIME1=05:36:27
TIME2=05:36:51
SEC1=`date +%s -d ${TIME1}`
SEC2=`date +%s -d ${TIME2}`
DIFFSEC=`expr ${SEC2} - ${SEC1}`
echo `date +%H:%M:%S -ud @${DIFFSEC}`
00:00:24
Can I use this set of lines inside a function, and call that function from awk?
Upvotes: 1
Views: 690
Reputation: 4887
You can do it using awk
, it's just a bit … unweildy:
awk -v cmd='date +%s -d ' -v cmd2='date +%H:%M:%S -d ' '/^000/ {
cmd $12 | getline T1; close(cmd $12);
cmd $16 | getline T2; close(cmd $16);
cmd2 (T2 - T1) | getline T1; close(cmd2 (T2 - T1));
printf "%-40s %-10s %-10s%-10s\n", $7, $12, $16, T1
}'
You can't use the shell's backtick process substitution in awk
. awk
has its own way of getting the output of a command - via getline
and coprocesses. The syntax is roughly:
command | getline var-name
close(command)
Where command
is a variable or string containing the command. cmd $12
is just the concatenation of cmd
and $12
, so the command would become date +%s -d 13:34:46
, for example.
date
interpretation of pure numbers as input is complex. It would be best to force it see the input number as a Unix timestamp, by using a leading @
:
awk -v cmd='date +%s -d ' -v cmd2='date +%H:%M:%S -d @' '/^000/ {
cmd $12 | getline T1; close(cmd $12);
cmd $16 | getline T2; close(cmd $16);
cmd2 (T2 - T1) | getline T3; close(cmd2 (T2 - T1));
printf "%-40s %-10s %-10s%-10s\n", $7, $12, $16, T3
}'
Upvotes: 2