Add numbers to the beginning of every line in a file

后端 未结 7 1704
暗喜
暗喜 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-01 03:04

    You're looking for the nl(1) command:

    $ nl -nrz -w9  /etc/passwd
    000000001   root:x:0:0:root:/root:/bin/bash
    000000002   daemon:x:1:1:daemon:/usr/sbin:/bin/sh
    000000003   bin:x:2:2:bin:/bin:/bin/sh
    ...
    

    -w9 asks for numbers nine digits long; -nrz asks for the numbers to be formatted right-justified with zero padding.

提交回复
热议问题