Explore Courses Blog Tutorials Interview Questions
0 votes
in Linux by (9.5k points)

I know the ‘nl’ command is used to number the lines if I code like below: 

$ cat test | nl  -i 2

    1   #! /bin/bash

    3   for dir in dir1 dir2

    5   do 

When I try to do the line-increment for float number, it throws an error as below: 

$ cat test | nl  -i 0.1

  nl: invalid line number increment: ‘0.1’

Below is my expected output: 

1   #! /bin/bash

1.1 for dir in dir1 dir2

1.2 do 

Can anyone tell me how to do it? 


1 Answer

0 votes
by (19.7k points)

You can try to use awk like below: 

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

If you want it do the printf for higher number like 99999 lines, you can do like below: 

awk '{printf "%-7.1f%s\n",0.9+(x=x+.1),$0}'

Interested in Linux? Check out this Linux Certification by Intellipaat. 

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Browse Categories