nl命令用法#

nl命令将文件输出时附带显示所有行号或部分行号,默认使用效果等同于cat -n

$ nl push.sh
     1  rm -rf resources/ public/ .hugo_build.lock
     2  git add .
     3  git commit -m "update"
     4  git push
     5  sleep 3s
     6  echo "Done!"
     7  sleep 3s

-n format选项用来设置编号样式,有以下三种

  • rn 右对齐,nl默认
  • ln 左对齐
  • rz 右对齐,0填充
$ nl -n ln push.sh
1       rm -rf resources/ public/ .hugo_build.lock
2
3
4       git add .
5       git commit -m "update"
6       git push
7       sleep 3s
8       echo "Done!"
9       sleep 3s

$ nl -n rn push.sh
     1  rm -rf resources/ public/ .hugo_build.lock
     2
     3
     4  git add .
     5  git commit -m "update"
     6  git push
     7  sleep 3s
     8  echo "Done!"
     9  sleep 3s

$ nl -n rz push.sh
000001  rm -rf resources/ public/ .hugo_build.lock
000002
000003
000004  git add .
000005  git commit -m "update"
000006  git push
000007  sleep 3s
000008  echo "Done!"
000009  sleep 3s

-s string选项用来设置编号和文本内容之间的分隔符,默认为TAB

$ nl -s :: push.sh
     1::rm -rf resources/ public/ .hugo_build.lock
     2::
     3::
     4::git add .
     5::git commit -m "update"
     6::git push
     7::sleep 3s
     8::echo "Done!"
     9::sleep 3s

-v number选项用来设置编号起始号,默认为1,也可以设置为负数

$ nl -v 5 push.sh
     5  rm -rf resources/ public/ .hugo_build.lock
     6
     7
     8  git add .
     9  git commit -m "update"
    10  git push
    11  sleep 3s
    12  echo "Done!"
    13  sleep 3s

$ nl -v -5 push.sh
    -5  rm -rf resources/ public/ .hugo_build.lock
    -4
    -3
    -2  git add .
    -1  git commit -m "update"
     0  git push
     1  sleep 3s
     2  echo "Done!"
     3  sleep 3s

-w number选项来设置编码位数宽度,默认为6

$ nl -w 2 push.sh -n rz
01      rm -rf resources/ public/ .hugo_build.lock
02
03
04      git add .
05      git commit -m "update"
06      git push
07      sleep 3s
08      echo "Done!"
09      sleep 3s
$ nl -w 8 push.sh -n rz
00000001        rm -rf resources/ public/ .hugo_build.lock
00000002
00000003
00000004        git add .
00000005        git commit -m "update"
00000006        git push
00000007        sleep 3s
00000008        echo "Done!"
00000009        sleep 3s