user5420138
user5420138

Reputation: 131

awk and for-loop with multiple variables

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

Upvotes: 2

Views: 1808

Answers (2)

David C. Rankin
David C. Rankin

Reputation: 84561

Another approach is to look backward from the current loop index and output based on $(i-2), $(i-1), $i. That would require a start of i=5, e.g.

awk -F, '{
    printf "echo %s ", $1
    for (i = 5; i <= NF; i += 3)
        printf " command1 %s command2 %s command3 %s", $(i-2), $(i-1), $i
    print ""
}' test.txt

In that case you produce the desired output:

echo "a"  command1 "c" command2 "d" command3 "e" command1 "f" command2 "g" command3 "h"
echo "m"  command1 "o" command2 "p" command3 "q" command1 "r" command2 "s" command3 "t"

Just another way to skin-the-cat with awk.

Do You Really Want The Extra command3?

Your output shows you want an additional command3 at the end. This seems quite awkward, but if it is needed, just add it in the final print where the newline is output as you have done, e.g.

     print "command3"

Your output is then:

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

Upvotes: 2

Walter A
Walter A

Reputation: 20002

Be careful with printf. The first argument is a formatstring, scanned for % characters.
With your input this buggy code will work:

awk -F"," '
{
    printf "echo %s ", $1
    for (i=3; i<=NF;i=i+3) {
        printf " command1 " $i " command2 " $(i+1) " command3 " $(i+2)
        }
    print " command3"
}' test.txt

Better is the changed printf:

awk -F"," '
{
    printf "echo %s ", $1
    for (i=3; i<=NF;i=i+3) {
        printf " command1 %s command2 %s command3 %s", $i, $(i+1), $(i+2)
        }
    print " command3"
}' test.txt

Upvotes: 2

Related Questions