Linux中打印文件行号的方法

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.
新手学习,有错误欢迎大家留言指出,后续知道其他的会陆续补充
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值