I am trying to write an awk script to read data from a txt file which requires a loop with three variables. Tried C like syntax and nested loops but no luck.
According to below link, C like syntax is also not supported with awk.
https://www.gnu.org/software/gawk/manual/html_node/For-Statement.html
The same is true of the increment part. Incrementing additional variables requires separate statements at the end of the loop. The C compound expression, using C’s comma operator, is useful in this context, but it is not supported in awk.
tst.awk
BEGIN { FS="," }
{
printf "echo %s ", $1
for (i=3; i<=NF;i=i+3) {
printf " command1 " $i " command2 " $4 " command3 " $5, $i
}
print "command3"
}
test.txt
"a","b","c","d","e","f","g","h"
"m","n","o","p","q","r","s","t"
Output:
echo "a" command1 "c" command2 "d" command3 "e" command1 "f" command2 "d" command3 "e"command3
echo "m" command1 "o" command2 "p" command3 "q" command1 "r" command2 "p" command3 "q"command3
Expected Output:
echo "a" command1 "c" command2 "d" command3 "e" command1 "f" command2 "g" command3 "h"command3
echo "m" command1 "o" command2 "p" command3 "q" command1 "r" command2 "s" command3 "t"command3