Linux中打印文件行号的方法

准备环境
[root@max mytest]# cat >/mytest/number.txt<<EOF
> no root,no fruit
> 
> seeing is believing.
> the day is short but the work is much.
> 
> in for a penny,in for a pound.
> EOF
方法1

cat -n直接打印显示文件行号

[root@max mytest]# cat -n /mytest/number.txt 
     1	no root,no fruit
     2	
     3	seeing is believing.
     4	the day is short but the work is much.
     5	
     6	in for a penny,in for a pound.
方法2

grep -n方法直接打印显示文件行号

[root@max mytest]# grep -n '.*' number.txt 
1:no root,no fruit
2:
3:seeing is believing.
4:the day is short but the work is much.
5:
6:in for a penny,in for a pound.
方法3

awk方法,print NR打印行号,print $0显示文件内容

[root@max mytest]# awk '{print NR,$0}' number.txt 
1 no root,no fruit
2 
3 seeing is believing.
4 the day is short but the work is much.
5 
6 in for a penny,in for a pound.
方法4

less -N打印系显示文件行号

[root@max mytest]# less -N number.txt 
      1 no root,no fruit
      2 
      3 seeing is believing.
      4 the day is short but the work is much.
      5 
      6 in for a penny,in for a pound.
方法5

在vim命令行模式中 :set nu显示行号,:set nonu取消显示

[root@max mytest]# vim number.txt 
  1 no root,no fruit
  2 
  3 seeing is believing.
  4 the day is short but the work is much.
  5 
  6 in for a penny,in for a pound.

:set nu
方法6

nl命令显示行号,但是这个命令会忽略空行

[root@max mytest]# nl number.txt 
     1	no root,no fruit
       
     2	seeing is believing.
     3	the day is short but the work is much.
       
     4	in for a penny,in for a pound.
新手学习,有错误欢迎大家留言指出,后续知道其他的会陆续补充
Logo

更多推荐