Search code examples
awktabular

awk print column for a given range


My data looks like that:

0.000000 0.071429 0.071429 0.857143 
0.071429 0.428571 0.071429 0.428571 
0.357143 0.214286 0.357143 0.071429 
0.000000 0.714286 0.000000 0.285714 
0.000000 0.571429 0.000000 0.428571 
0.428571 0.357143 0.071429 0.142857 
0.000000 0.071429 0.071429 0.857143 
0.071429 0.000000 0.928571 0.000000 
0.000000 0.071429 0.000000 0.928571 
0.000000 0.285714 0.000000 0.714286 
0.142857 0.000000 0.785714 0.071429 

I want it to look like that:

AC name_of_the_file.txt
00 0.000000 0.071429 0.071429 0.857143 
01 0.071429 0.428571 0.071429 0.428571 
02 0.357143 0.214286 0.357143 0.071429 
03 0.000000 0.714286 0.000000 0.285714 
04 0.000000 0.571429 0.000000 0.428571 
05 0.428571 0.357143 0.071429 0.142857 
06 0.000000 0.071429 0.071429 0.857143 
07 0.071429 0.000000 0.928571 0.000000 
08 0.000000 0.071429 0.000000 0.928571 
09 0.000000 0.285714 0.000000 0.714286 
10 0.142857 0.000000 0.785714 0.071429  
XX
//

How can I awk $1 for a range (from 00 till the file ends)?


Solution

  • One way:

    awk 'FNR == 1 { print FILENAME } { printf "%02d %s\n", FNR - 1, $0 }' infile
    

    Output:

    infile
    00 00 0.000000 0.071429 0.071429 0.857143 
    01 01 0.071429 0.428571 0.071429 0.428571 
    02 02 0.357143 0.214286 0.357143 0.071429 
    03 03 0.000000 0.714286 0.000000 0.285714 
    04 04 0.000000 0.571429 0.000000 0.428571 
    05 05 0.428571 0.357143 0.071429 0.142857 
    06 06 0.000000 0.071429 0.071429 0.857143 
    07 07 0.071429 0.000000 0.928571 0.000000 
    08 08 0.000000 0.071429 0.000000 0.928571 
    09 09 0.000000 0.285714 0.000000 0.714286 
    10 10 0.142857 0.000000 0.785714 0.071429