How to execute a command for each loop in awk?
I'm using awk to split a binary stream and I can get each part into a for loop like this.
for(i=1;i<=NF;i++)
I don't want to convert each field to text or arguments, but simply pass it directly to a command.
I'm trying to find something like this,
for(i=1;i<=NF;i++) system("decode")
but this obviously doesn't work. decode receives no input.
How do I get decode to receive each field in the loop?
Doesn't this works for you?
for(i=1;i<=NF;i++) print $i | "decode"
close("decode")
It sends each field (or byte in your case) to a pipe connected to the "decode" program.
After that, close the "decode" pipe to force a flush of all the data on that pipe.
You can read more about gawk redirection on https://www.gnu.org/software/gawk/manual/html_node/Redirection.html
If you want to execute "decode" for each single byte, just close the pipe in each iteration:
for(i=1;i<=NF;i++) {
print $i | "decode"
close("decode")
}
Is this what you are trying to do?
awk '{for(i=1; i<=NF; i++) system("decode "$i)}' input_file.txt
This should pass each field, contained in awk variable $i
, to the decode
external program. Mind the variable is outside the quotes for "decode "
, or it would be interpreted by the shell instead of awk.
下一篇: 如何在awk中为每个循环执行一个命令?