How to execute command for each loop in awk?

I use awk to split the binary stream, and I can get every part in a for loop like this.

for(i=1;i<=NF;i++)

I do not want to convert each field to text or arguments, but just pass it directly to the command.

I'm trying to find something like this,

for(i=1;i<=NF;i++) system("decode")

but this obviously does not work. decoding does not accept input.

How to get decoding to get each field in a loop?

+8
binary-data awk
source share
2 answers

Doesn't this work for you?

 for(i=1;i<=NF;i++) print $i | "decode" close("decode") 

It sends each field (or byte in your case) to the channel connected to the "decoding" program.

After that, close the β€œdecode” pipe to force reset all data on this channel.

You can learn more about redirecting gawk to https://www.gnu.org/software/gawk/manual/html_node/Redirection.html

If you want to perform "decoding" for each individual byte, just close the channel at each iteration:

 for(i=1;i<=NF;i++) { print $i | "decode" close("decode") } 
+3
source share

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 the awk $i variable to an external decode program. Remember that the variable is outside the quotes for "decode " , or it will be interpreted by the shell instead of awk.

+1
source share

All Articles