user2268507
user2268507

Reputation:

awk: run time error: negative field index

I currently have the following:

function abs() {
    echo $(($1<0 ?-$1:$1));
}

echo $var1 | awk -F" " '{for (i=2;i<=NF;i+=2) $i=(95-$(abs $i))*1.667}'

where var1 is:

4 -38 2 -42 1 -43 10 -44 1 -45 6 -46 1 -48 1 -49    

When I run this, I am getting the error:

awk: run time error: negative field index $-38
FILENAME="-" FNR=1 NR=1

Does this have something to do with the 95-$(abs $i) part? I'm not sure how to fix this.

Upvotes: 0

Views: 1464

Answers (2)

Every line of input to AWK is placed in fields by the interpreter. The fields can be accessed with $N for N > 0. $0 means the whole line. $N for N < 0 is nonsensical. Variables are not prefixed with a dollar sign.

Upvotes: 0

ooga
ooga

Reputation: 15501

Try this:

echo "$var1" |
awk 'function abs(x) { return x<0 ? -x : x }
     { for (i=2;i<=NF;i+=2) $i = (95-abs($i))*1.667; print }'

Upvotes: 3

Related Questions