Reputation: 4623
I would like to read data from stdin and execute a shell command for each parsed line and recover its output.
Here an example which almost works:
[root@eulbi002] # ping eulbi001 | awk -F'[ =]' '/64 bytes/{"date +%s"|getline D; print D,$11}'
1360069298 0.056
1360069298 0.051
1360069298 0.051
The command 'date +%s' is executed and I can recover the output, but it looks to me like the execution happens only once, not on every match.
The ultimate aim is to hand the output over to rrdtool for storage and rrdtool wants a timestamp for each record.
@Ed Morton, in addition to point out a working solution, mentioned gawk with its built-in time functions. This is the most elegant solution.
[root@eulbi002] # ping eulbi001 | awk -F'[ =]' '/64 bytes/{print systime(),$11}'
1360069298 0.056
1360069298 0.051
1360069298 0.051
Upvotes: 1
Views: 3941
Reputation: 204259
Just in case there's yet another getline caveat kicking in here (see http://awk.info/?tip/getline), try closing the pipe after every invocation, e.g.:
ping eulbi001 |
awk -F'[ =]' 'BEGIN{cmd="date +%s"} /64 bytes/{cmd|getline D; close(cmd); print D,$11}'
and see if you get different results. You still shouldn't expect D to change on every invocation though, just every second. You could add a ".%N" for smaller granularity of your timestamps if you like.
Better yet, use GNU awk with it's builtin time functions so you don't need to deal with this stuff at all.
Upvotes: 4