Add numbers to the beginning of every line in a file

后端 未结 7 1705
暗喜
暗喜 2020-12-01 02:44

How can I add numbers to the beginning of every line in a file?

E.g.:

This is
the text
from the file.

Becomes:

000000001 This is
000         


        
7条回答
  •  南笙
    南笙 (楼主)
    2020-12-01 03:13

    AWK's printf, NR and $0 make it easy to have precise and flexible control over the formatting:

    ~ $ awk '{printf("%010d %s\n", NR, $0)}' example.txt
    0000000001 This is
    0000000002 the text
    0000000003 from the file.
    

提交回复
热议问题