题目是这样的,Linux中一个文件10行内容,如何输出5-8内容到屏幕
首先我们模拟一下这样的环境:
[root@localhost question]# pwd
/root/question
[root@localhost question]# seq 1 10 > q.txt
[root@localhost question]# cat q.txt
1
2
3
4
5
6
7
8
9
10
我们的任务是取5-8行输出:
第一种方法:
[root@localhost question]# sed -n '5,8p' q.txt
5
6
7
8
第二种方法:
[root@localhost question]# awk 'NR>4 && NR <9' q.txt
5
6
7
8
第三种方法:
[root@localhost question]# grep 5 q.txt -A 3
5
6
7
8
第四种方法:
[root@localhost question]# grep 8 q.txt -B 3
5
6
7
8
其本质就是用grep,awk,sed这Linux三剑客来实现,更想说的是这三个命令可以说是Linux基础命令的核心。
转载于:https://blog.51cto.com/652465/2056067