使用grep找到某个文件及其相邻行 我有个文件列表按时间排列。别如
ls -lrt
现在想用grep找到名字为 file1的那行,并且显示其上下相邻的行。请问该怎么写?
最好能写成一句, 比如
ls -lrt | grep ....
分享到:更多
------解决方案--------------------
ls -lrt ------解决方案-------------------- grep -C 1 "file1"
-A NUM, --after-context=NUM
Print NUM lines of trailing context after matching lines. Places a line containing -- between contiguous groups of matches.
-B NUM, --before-context=NUM
Print NUM lines of leading context before matching lines. Places a line containing -- between contiguous groups of matches.
-C NUM, --context=NUM
Print NUM lines of output context. Places a line containing -- between contiguous groups of matches. ------解决方案-------------------- grep 的 -A -B和-C参数就可以了
% grep -A <n> 'keyword' file # 匹配 keyword 的下 n 行
% grep -B <n> 'keyword' file # 匹配 keyword 的上 n 行
% grep -C <n> 'keyword' file # 匹配 keyword 的上 n 行及下 n 行