方法1: (head 和 tail通过管道组合)

[root@VM_179_129_centos tmp]# head -30 ett.txt | tail -11

20

21

22

23

24

25

26

27

28

29

30

1

2

3

4

5

6

7

8

9

10

11

12

命令解释:head -n 30 xxx.txt == head -30 xxx.txt 取文件前30行内容

tail -11 xxx.txt 取文件后11行内容

| 管道命令连接 将head取出的30行内容作为tail的输入

方法2: awk命令

[root@VM_179_129_centos tmp]# awk 'NR==20,NR==30' ett.txt

20

21

22

23

24

25

26

27

28

29

30

1

2

3

4

5

6

7

8

9

10

11

12

awk命令中 NR表示行号,直译 取20-30行的内容

awk ‘NR==35’ ett.txt 取第35行内容

方法3:sed命令

[root@VM_179_129_centos tmp]# sed -n '20,30p' ett.txt

20

21

22

23

24

25

26

27

28

29

30

1

2

3

4

5

6

7

8

9

10

11

12

sed命令 中-n 参数搭配p 一起来使用

1.打印文件的第二行

sed -n ‘2p’ file

2.打印1到3行

sed -n ‘1,3p’ file

3.品配单词用/patten/模式,eg,/Hello/

sed -n ‘/Hello/’p file

4.使用模式和行号进行品配,在第4行查询Hello

sed -n ‘4,/Hello/’ file

5.配原字符(显示原字符$之前,必须使用\屏蔽其特殊含义)

sed -n ‘/$/’p file

上述命令将把file中含有$的行打印出来

6.显示整个文件(只需将范围设置为1到最后于一行)

$代表最后一行

sed -n ‘1,$p’ file

7.任意字符 ,模式/.*/,如/.*ing/匹配任意以ing结尾的单词

sed -n ‘/.*ing/’p file

8.打印首行

sed -n ‘1p’ file

9.打印尾行

sed -n ‘$p’ file

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐