linux中使用head,tail,grep, sed,awk三种方法显示文档中间若干行(指定任意行)

 

需要显示文本中间20-25行.

创建一个30行的文档,命名为30.txt并显示在屏幕

[root@v2-ui data]# seq 30 > 30.txt && cat 30.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
[root@v2-ui data]#
View Code

 

方法一:

[root@v2-ui data]# head -25 30.txt | tail -6
20
21
22
23
24
25
[root@v2-ui data]# 
View Code

补充

[root@v2-ui data]# tail -n +20 30.txt | head -6
20
21
22
23
24
25
[root@v2-ui data]# ^C

1.tail -n 10 打印文件最后10行的数据

2.tail -n +10 打印文件第10行开始以后的内容

3.head -n 10打印前10的内容

4.head -n +10 同上

方法二:

[root@v2-ui data]# sed -n "20,25"p 30.txt
20
21
22
23
24
25
[root@v2-ui data]#
View Code

-n表示第XX行,p打印的意思

方法三:

1 [root@v2-ui data]# awk "NR>19 && NR <26" 30.txt 
2 20
3 21
4 22
5 23
6 24
7 25
8 [root@v2-ui data]# 

NR表示行号

posted on 2019-12-30 10:18  感谢蛋炒饭  阅读(1198)  评论(0编辑  收藏  举报

导航