linux中如何给数据添加行号

 

1、测试数据

root@DESKTOP-1N42TVH:/home/test2# ls
test.txt
root@DESKTOP-1N42TVH:/home/test2# cat test.txt
i j k
x c b
z c b
e g a

 

2、输出行序号

root@DESKTOP-1N42TVH:/home/test2# ls
test.txt
root@DESKTOP-1N42TVH:/home/test2# cat test.txt
i j k
x c b
z c b
e g a
root@DESKTOP-1N42TVH:/home/test2# cat -A test.txt
i j k$
x c b$
z c b$
e g a$
root@DESKTOP-1N42TVH:/home/test2# grep -n "." test.txt      ## grep实现
1:i j k
2:x c b
3:z c b
4:e g a
root@DESKTOP-1N42TVH:/home/test2# cat -n test.txt           ## cat实现
     1  i j k
     2  x c b
     3  z c b
     4  e g a
root@DESKTOP-1N42TVH:/home/test2# nl test.txt               ## nl实现
     1  i j k
     2  x c b
     3  z c b
     4  e g a
root@DESKTOP-1N42TVH:/home/test2# awk '{print NR}' test.txt    ## awk实现
1
2
3
4
root@DESKTOP-1N42TVH:/home/test2# sed -n = test.txt             ## sed实现
1
2
3
4

 

root@DESKTOP-1N42TVH:/home/test2# ls
test.txt
root@DESKTOP-1N42TVH:/home/test2# cat test.txt
i j k
x c b
z c b
e g a
root@DESKTOP-1N42TVH:/home/test2# sed = test.txt
1
i j k
2
x c b
3
z c b
4
e g a
root@DESKTOP-1N42TVH:/home/test2# sed = test.txt | sed 'N; s/\n/ /'       ## sed实现
1 i j k
2 x c b
3 z c b
4 e g a

 

posted @ 2022-04-26 15:10  小鲨鱼2018  阅读(391)  评论(0编辑  收藏  举报