我想在通常有很长行的HTML文件上运行ack或grep.我不想看到很长的线条反复包裹.但我确实希望看到围绕与正则表达式匹配的字符串的长行的那一部分.如何使用任何Unix工具组合获得此功能?
Eth*_*her 82
您可以使用grep选项-o
,可能与更改模式相结合,".{0,10}<original pattern>.{0,10}"
以便查看它周围的一些上下文:
-o, --only-matching Show only the part of a matching line that matches PATTERN.
..或-c
:
-c, --count Suppress normal output; instead print a count of matching lines for each input file. With the -v, --invert-match option (see below), count non-matching lines.
And*_*ter 39
通过管道你的结果cut
.我也在考虑添加一个--cut
开关,所以你可以说--cut=80
只有80列.
Jon*_*aun 21
您可以使用less作为ack的寻呼机并切断长线:ack --pager="less -S"
这样可以保留长线但是将其留在一条线而不是包裹.要查看更多行,请使用箭头键向左/向右滚动.
我为ack做了以下别名设置:
alias ick='ack -i --pager="less -R -S"'
Run Code Online (Sandbox Code Playgroud)
grep -oE ".\{0,10\}error.\{0,10\}" mylogfile.txt
在无法使用的异常情况下,请-E
改用小写字母-e
。