Answers:
你可以使用Terminal和这个shell函数:
cless ()
{
# less-like function that colors specified word
# 'while read' reads every line and saves it to variable $REPLY
while read; do
# If line contains the word to highlight, grep with option --color...
if [ $(echo $REPLY | grep -c $1) -eq 1 ]; then
echo $REPLY | grep --color=always $1;
else
# ... otherwise simply output the line
echo $REPLY;
fi;
# continue until end of file, paging with 'less'
done < $2 | less -R
}
它需要两个参数,要突出显示的单词和要解析的文件。
要使用它,请在应用程序>实用程序>终端中打开终端并键入;
$ function cless { while read; do if [ $(echo $REPLY | grep -c $1) -eq 1 ]; then echo $REPLY | grep --color=always $1; else echo $REPLY; fi; done < $2 | less -R; }
$ cless ERROR /path/to/my/logfile.log
与less
使用SPACE或F向前B滚动并向后滚动一样。如果您经常查找的单词是ERROR,请创建别名:
$ alias cerror='cless ERROR'
$ cerror /path/to/my/logfile.log
要在启动终端时自动加载函数和别名,请将以下行添加到~/.bashrc
文件中:
function cless { while read; do if [ $(echo $REPLY | grep -c $1) -eq 1 ]; then echo $REPLY | grep --color=always $1; else echo $REPLY; fi; done < $2 | less -R; }
alias cerror='cless ERROR'
并重新加载它:
$ . ~/.bashrc
如果您希望不区分大小写的搜索,请替换grep
为grep -i
(例如,grep -c $1
将成为grep -i -c $1
)。