给文本文件加行号的方式有哪些

方法一:用cat简单实现

cat -n FileName

[root@localhost test]# cat -n test
     1	one
     2	two
     3	three
     4	four
     5	five
     6	six
     7	seven
     8	eight
     9	nine
    10	ten

方法二:grep处理

[root@localhost test]# grep -n ".*" test | sed -n 's/:/\t/p'
1	one
2	two
3	three
4	four
5	five
6	six
7	seven
8	eight
9	nine
10	ten

方法三:sed处理

命令首先使用sed命令的“=”命令为每一行添加行号,然后使用“N”命令将两行内容合并成一行。接着使用替换命令“s”将原来的换行符替换成一个空格

[root@localhost test]# sed = test | sed -n 'N;s/\n/\t/p'
1	one
2	two
3	three
4	four
5	five
6	six
7	seven
8	eight
9	nine
10	ten

方法四:ln命令处理

1、将会将test中的内容添加行号后输出到newtest.txt文件中
[root@localhost test]# nl test > newtest.txt
[root@localhost test]# cat newtest.txt
     1	one
     2	two
     3	three
     4	four
     5	five
     6	six
     7	seven
     8	eight
     9	nine
    10	ten
2、自定义行号的格式,可以使用nl命令的选项来实现
[root@localhost test]# nl -s '|' -n ln test > newtest.txt
[root@localhost test]# cat newtest.txt
1     |one
2     |two
3     |three
4     |four
5     |five
6     |six
7     |seven
8     |eight
9     |nine
10    |ten

方法五:直接在vim中使用命令模式

:set nu

[root@localhost test]# vim test

 1 one
  2 two
  3 three
  4 four
  5 five
  6 six
  7 seven
  8 eight
  9 nine
 10 ten
~                                                        
~                                                        
~                                                        
~                                                        
~                                                        
~                                                        
~                                                        
~                                                        
~                                                        
~                                                        
~                                                        
~                                                        
:set nu            

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值