AishwaryaKulkarni AishwaryaKulkarni - 6 months ago 31
Bash Question

Addition in awk failing

I am using following code snippet where I export the shell variables in awk as follows:

half_buffer1=$((start_buffer/2))
half_buffer2=$((end_buffer/2))
echo $line | awk -v left="$half_buffer1" -v right="$half_buffer2" 'BEGIN {print $1"\t"$2-left"\t"$3+right"\t"$4"\t"$5"\t"$6"\t"$7"\t"$8}'


However for the variable 'right' in awk at times the $3 variable is being subtracted from instead of adding the 'right' variable to $3.

Answer

Observe that the following provides the "wrong" answers:

$ echo 1 2 3 4 5 | awk -v left=10 -v right=20 'BEGIN {print $1"\t"$2-left"\t"$3+right"\t"$4"\t"$5"\t"$6"\t"$7"\t"$8}'
        -10     20

To get the right answers, remove BEGIN:

$ echo 1 2 3 4 5 | awk -v left=10 -v right=20 '{print $1"\t"$2-left"\t"$3+right"\t"$4"\t"$5"\t"$6"\t"$7"\t"$8}'
1       -8      23      4       5

The problem is that the BEGIN block is executed before any input is read. Consequently, the variables $1, $2, etc., do not yet have useful values.

If BEGIN is removed, the code is executed on each line read. This gives you the answers that you want.

Examples

Using real input lines from the comments:

$ echo ID1 14389398 14389507 109 + ABC 608 831 | awk -v left=10 -v right=20 '{print $1"\t"$2-left"\t"$3+right"\t"$4"\t"$5"\t"$6"\t"$7"\t"$8}'
ID1     14389388        14389527        109     +       ABC     608     831

$ echo  ID1 14390340 14390409 69 + ABC 831 32 –  | awk -v left=10 -v right=20 '{print $1"\t"$2-left"\t"$3+right"\t"$4"\t"$5"\t"$6"\t"$7"\t"$8}'
ID1     14390330        14390429        69      +       ABC     831     32

Also, this shell script:

start_buffer=10
end_buffer=100
half_buffer1=$((start_buffer/2))
half_buffer2=$((end_buffer/2))
echo  ID1 14390340 14390409 69 + ABC 831 32 –  | awk -v left="$half_buffer1" -v right="$half_buffer2" '{print $1"\t"$2-left"\t"$3+right"\t"$4"\t"$5"\t"$6"\t"$7"\t"$8}'

produces this output:

ID1     14390335        14390459        69      +       ABC     831     32