[英]Using bash/tail/perl/alias for easy highlighting of different strings
I am developing a tomcat application and would like to be able to search for specific things and highlight it when viewing the log. 我正在开发一个tomcat应用程序,并希望能够搜索特定的东西并在查看日志时突出显示它。 I want something like an alias that takes a parameter (regex) as input and highlight the matching string. 我想要一个像参数(正则表达式)作为输入的别名,并突出显示匹配的字符串。
So far, I've figured this works, but its not practical enough to have to change a small part of it for every time I want something new: 到目前为止,我认为这是有效的,但它不够实用,每次我想要一些新东西时都要改变它的一小部分:
tail -n 100 -f /opt/apache-tomcat-6.0.26/logs/catalina.out | perl -pe 's/null/\e[1;31m$&\e[0m/g'
This is what I thought would work: 这是我认为会起作用的:
logColor(){
x="'s/"
y="/\e[1;31m$&\e[0m/g'"
tail -n 100 -f /opt/apache-tomcat-6.0.26/logs/catalina.out | perl -pe $x$1$y
}
alias logC=logColor
I've tested that this prints out the two same lines: 我测试过这打印出两条相同的线:
logColorTest(){
x="'s/"
y="/\e[1;31m$&\e[0m/g'"
echo $x$1$y
echo "'s/null/\e[1;31m$&\e[0m/g'"
}
alias logCT=logColorTest
logCT null
So I am lost on why this does not work and would appreciate input from someone who knows how this works :) 所以我迷失了为什么这不起作用,并希望得到知道如何工作的人的意见:)
Problem with grep
is that, you get only matching lines & other lines are filtered out. grep
问题在于,您只获得匹配的行和其他行被过滤掉。 (That's what is grep
supposed to do anyway.) Many times however, we need all the output, but with some particular strings highlighted. (这就是grep
应该做的事情。)但是很多时候,我们需要所有的输出,但突出显示了一些特定的字符串。
I have this small bash function in my .bashrc
for such requirement: 我在.bashrc
有这个小bash函数用于这样的要求:
mark ()
{
local searchExpr=${1/\//\\\/};
sed "s/$searchExpr/"`echo -n -e "\e[91;1m"`'&'`echo -n -e "\e[0m"`'/gi' $2
}
Usage: 用法:
command | mark some_string # OR
mark some_string some_file
Rename to suitable function name if required. 如果需要,重命名为合适的函数名称。
NOTE: There is a great command called highlight
. 注意:有一个很棒的命令叫做highlight
。 Hence I could not use that as my function name. 因此我无法将其用作我的函数名称。
As @fedorqui pointed out, you can use grep
to do this: 正如@fedorqui指出的那样,你可以使用grep
来做到这一点:
grep --colour 'null\|$'
This will match and highlight null
or the end of a line, meaning all lines are shown. 这将匹配并突出显示null
或行的结尾,这意味着显示所有行。
Using the GREP_COLORS
environment variable you can control how different parts are highlighted, eg mark matched text in yellow: 使用GREP_COLORS
环境变量,您可以控制突出显示不同部分的方式,例如将匹配的文本标记为黄色:
export GREP_COLORS='ms=1;33'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.