I'm trying to get result of a ping from TCL
in AWK
with the following code:
set n [exec ping 8.8.8.8 -c 2]
exec awk {"NR>=2 && NR<=4 { print }"} $n
but I get this error:
Error in startup script: awk: can't open file PING 8.8.8.8 ...
it seems that AWK
gets $n
not as an input but as a file.
I found this but although it seems the same question, I'm having different problem. I can RUN AWK
inside my TCL
. The problem is that it does't accept Input
as a source from me and although I didn't us -f
switch it still gets it as a file.
2 choices come to mind
hand over the ping output as the stdin to awk:
set n [exec ping 8.8.8.8 -c 2]
exec awk {NR>=2 && NR<=4 { print }} << $n
# ..................................^^
don't bother with the temporary variable
exec ping 8.8.8.8 -c 2 | awk {NR>=2 && NR<=4 { print }}
You don't want the double quotes inside the braces: the braces are used to quote the awk body, and you don't want to pass the double quotes to awk.
Another option: since you just want the 2nd through 4th lines of the ping output, use Tcl and you don't need awk at all:
set ping [exec ping 8.8.8.8 -c 2]
set lines [lrange [split $ping \n] 1 3]
puts [join $lines \n]
or unreadably
puts [join [lrange [split [exec ping 8.8.8.8 -c 2] \n] 1 3] \n]