grep-like工具,颜色/ boldens但不过滤?

时间:2012-02-22 16:37:44

标签: regex colors sed grep term

多年来我一直注意到GNU在Unix中的grep(不仅仅是Linux风格)检测到我正在使用像xterm / gnome-terminal / kconsole这样的具有颜色功能的TERM,并在上下文中很好地着色了我的搜索表达式。 / p>

什么可能是一个简单的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

在管道的末尾(|column -t之后),最好添加至少突出显示字符串$(date +%Y%m%d)的内容,目前评估为20120222

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

4 个答案:

答案 0 :(得分:27)

您可以使用egrep并匹配您正在寻找的模式和行首。由于后者总是匹配,但不能着色,它模仿你所追求的。

egrep 'pattern|^' file

编辑:我最初是从this question的答案中学到的。

答案 1 :(得分:10)

grep有一个-C选项,可以显示匹配行周围的上下文。你可以利用它。

grep -C 9999 'pattern' file

答案 2 :(得分:6)

你正在寻找的sed技巧应该或多或少是这样的:

sed 's/pattern/\x1b[01;31m&\x1b[0m/g' file

其中:

  • pattern与您在grep
  • 中使用的相同 替换模式中的
  • &表示使用与匹配
  • 相同的文本
  • 用于&的字符用于将前景色设置为红色并在之后重置

有关如何在此question的答案中sed中转义颜色序列的详细信息。

答案 3 :(得分:1)

扩展shellter提到的less选项(如果您只想直观地检查彩色输出而不是将其传递给另一个程序,则该选项效果很好),步骤如下:

  1. 将输出传输到less(或运行less filename.txt)。
  2. /向前搜索(或?向后搜索)。
  3. 输入您的搜索字词(默认为正则表达式,或先按Ctrl-r切换为使用简单字符串)。按Enter
  4. 搜索字词将突出显示。按n移至下一个匹配项,或按N移至上一个匹配项。
  5. q退出。