首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 操作系统 > UNIXLINUX >

使用grep找出某个文件及其相邻行

2013-12-10 
使用grep找到某个文件及其相邻行我有个文件列表按时间排列。别如ls -lrt现在想用grep找到名字为 file1的那

使用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 行

[解决办法]
引用:
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 行


++

热点排行