Skip to content
Advertisement

set line-increment in nl command to a float number

I want to use nl command to do numbering for lines. Is it possible to set line-increment in nl command to a float number.
I know that we cant use -i switch to set line-increment like this:

$ cat test | nl  -i 2
    1   #! /bin/bash
    3   for dir in dir1 dir2
    5   do 

but it doesn’t work for float numbers like:

$ cat test | nl  -i 0.1
  nl: invalid line number increment: ‘0.1’

I want the output to be like this

1   #! /bin/bash
1.1 for dir in dir1 dir2
1.2 do 

Advertisement

Answer

Could use awk instead

awk '{print 0.9+(x=x+.1) FS $0}' file

With padding (up to 99999 lines, for more add higher number in printf)

awk '{printf "%-7.1f%sn",0.9+(x=x+.1),$0}'
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement