正则表达式 – 类似grep的工具,颜色/加粗但不过滤?

多年来,我注意到Unix中的GNU grep(不仅仅是 Linux风格)检测到我正在像xterm / gnome-terminal / kconsole这样交互地使用一个可以支持色彩的TERM,并且在上下文中很好地染色了我的搜索表达式.

什么可能是一个简单的cli工具,只是粗体/突出显示您的正则表达式或简单的搜索字符串,但与grep不同,否则不会过滤出任何行?也许一个sed技巧可以做到这一点,我可以别名和以后的管道命令?

所以我写了一个程序pricehist.sh,它通常会为另一个程序生成类似CSV的数据,但是我经常直观地检查它:

./pricehist.sh mostalexp-GPW  28 |sort -r -g -k 3 -t ' ' |column -t -s", "
MOSTALEXP,20120222,0.78,0.79,0.76,0.77,63900,0.77    0.04000   5.00000   -0.01000  -1.25000  49203
MOSTALEXP,20120203,0.82,0.82,0.78,0.78,446089,0.78   0.04000   4.87805   0         0         347949
MOSTALEXP,20120124,0.71,0.71,0.69,0.71,61478,0.71    0.03000   4.16667   -0.01000  -1.38889  43649
MOSTALEXP,20120130,0.73,0.73,0.70,0.73,59488,0.73    0.03000   4.10959   0         0         43426
MOSTALEXP,20120210,0.79,0.81,0.77,0.81,104120,0.81   0.03000   3.75000   0.01000   1.25000   84337
MOSTALEXP,20120213,0.80,0.80,0.78,0.80,86606,0.80    0.03000   3.70370   -0.01000  -1.23457  69284
MOSTALEXP,20120126,0.69,0.75,0.69,0.72,146353,0.72   0.02000   2.81690   0.04000   5.63380   105374
MOSTALEXP,20120131,0.72,0.73,0.71,0.72,72734,0.72    0.02000   2.73973   0         0         52368
MOSTALEXP,20120123,0.72,0.73,0.71,0.72,24133,0.72    0.02000   2.73973   0         0         17375
MOSTALEXP,20120214,0.78,0.81,0.78,0.79,56990,0.79    0.02000   2.50000   0.01000   1.25000   45022
MOSTALEXP,20120209,0.79,0.82,0.79,0.80,65243,0.80    0.02000   2.46914   0.01000   1.23457   52194
MOSTALEXP,20120118,0.69,0.70,0.68,0.69,62520,0.69    0.01000   1.44928   0.01000   1.44928   43138
MOSTALEXP,20120125,0.70,0.71,0.70,0.71,92317,0.71    0.01000   1.40845   0         0         65545
MOSTALEXP,20120201,0.71,0.74,0.71,0.74,199791,0.74   0.01000   1.38889   0.02000   2.77778   147845
MOSTALEXP,20120127,0.73,0.73,0.71,0.73,38157,0.73    0.01000   1.38889   0.01000   1.38889   27854
MOSTALEXP,20120207,0.78,0.79,0.77,0.79,56243,0.79    0.01000   1.28205   0.01000   1.28205   44431
MOSTALEXP,20120206,0.78,0.80,0.77,0.78,133597,0.78   0.01000   1.28205   0.02000   2.56410   104205
MOSTALEXP,20120221,0.81,0.81,0.79,0.80,112738,0.80   0.01000   1.25000   0.01000   1.25000   90190
MOSTALEXP,20120217,0.80,0.85,0.79,0.80,272245,0.80   0.01000   1.25000   0.05000   6.25000   217796
MOSTALEXP,20120216,0.81,0.81,0.79,0.80,30814,0.80    0.01000   1.25000   0.01000   1.25000   24651
MOSTALEXP,20120113,0.66,0.66,0.65,0.65,90267,0.65    0.00500   0.76336   0.00500   0.76336   58673
MOSTALEXP,20120220,0.80,0.82,0.80,0.80,83881,0.80    0         0         0.02000   2.50000   67104
MOSTALEXP,20120215,0.80,0.82,0.79,0.80,128714,0.80   0         0         0.03000   3.79747   102971
MOSTALEXP,20120208,0.79,0.84,0.79,0.81,280446,0.81   0         0         0.05000   6.32911   227161
MOSTALEXP,20120120,0.74,0.74,0.72,0.73,128831,0.73   0         0         0.02000   2.77778   94046
MOSTALEXP,20120117,0.67,0.71,0.67,0.69,284886,0.69   0         0         0.04000   5.97015   196571
MOSTALEXP,20120116,0.65,0.67,0.65,0.67,9548,0.67     0         0         0.02000   3.07692   6397
MOSTALEXP,20120202,0.75,0.87,0.75,0.82,1270955,0.82  -0.01000  -1.35135  0.13000   17.56757  1042183
MOSTALEXP,20120119,0.70,0.74,0.70,0.72,320471,0.72   -0.01000  -1.44928  0.05000   7.24638   230739

在管道末尾(之后的列 – -t),添加一些至少突出显示至少字符串$(日期%Y%m%d)的东西是很好的,目前这个字符串估值为20120222

另一个例子是tail -F /tmp/*.log | grep -C 9999 YouJustGotHacked除了我不希望grep实际过滤掉任何行,只是让YouJustGotHacked更容易发现噪音.

您可以使用egrep并匹配您要查找的模式和行开始.由于后者总是匹配,而是不能被着色,它会模拟你所追求的.

egrep 'pattern|^' file

编辑:我原来从this question的答案中了解到.

相关文章
相关标签/搜索