Search code examples
awkdata-processing

My awk script don't print 0 and NR with 0


This is my script. How to force my script to print also zero. It is very important to me. I don,t know why it is not print zero, I add a+0, so it should!

#!/bin/bash
awk 'a=$1/617 {print NR " " a+0}' sum.txt | tee autocor.txt

This is my input

1
0
0
1
0
0
0
0
1

and this is my output

1 0.00162075
4 0.00162075
9 0.00162075

Expected output

1 0.00162075
2 0
3 0
4 0.00162075
5 0
6 0
7 0
8 0
9 0.00162075

Solution

  • Your a = $1 / 617 condition will not be fulfilled when $1 == 0 (because this value is considered falsy in awk). If you want to print every result just do not add any condition to your rule:

    cat input | awk '{ print NR " " $1/617 }'
    

    Will print

    1 0.00162075
    2 0
    3 0
    4 0.00162075
    5 0
    6 0
    7 0
    8 0
    9 0.00162075