1.题目
给定一个文本文件 file.txt,请只打印这个文件中的第十行。
示例:
假设 file.txt 有如下内容:
Line 1Line 2Line 3Line 4Line 5Line 6Line 7Line 8Line 9Line 10
你的脚本应当显示第十行:
Line 10
说明:
- 如果文件少于十行,你应当输出什么?
- 至少有三种不同的解法,请尝试尽可能多的方法来解题。
2.思路
打印第十行:sed -n '10p' file.txt
打印一到十行:sed -n '1,10p' file.txt
查找指定字符:grep -n 'KeyWord' file.txt
打印指定字符上下5行:grep -C 5 'KeyWord' file.txt
打印指定字符上下N行:grep -A 100 -B 100 'KeyWord' file.txt
(-A after 后面, -B before 前面)
查找指定字符出现次数: grep -o 'KeyWord' file.txt | wc -l
