17

多年来,我注意到 GNU 在 Unix 中的 grep(不仅仅是 Linux 风格)检测到我正在交互地使用具有颜色功能的 TERM,例如 xterm/gnome-terminal/kconsole,并且很好地在上下文中为我的搜索表达式着色。

什么可能是一个简单的 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)添加一些至少突出显示 string 的东西会很好,$(date +%Y%m%d)目前评估为20120222

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

4

4 回答 4

32

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

egrep 'pattern|^' file

编辑:我最初是从这个问题的答案中学到的。

于 2012-02-22T18:03:36.357 回答
11

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

grep -C 9999 'pattern' file
于 2012-02-22T16:59:03.720 回答
7

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

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

在哪里:

  • pattern与您将使用的相同grep
  • &在替换模式中意味着使用与匹配相同的文本
  • 周围的字符&用于将前景色设置为红色,然后将其重置

有关如何在sed问题的答案中转义颜色序列的更多信息。

于 2012-02-22T16:55:19.473 回答
1

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

  1. 将您的输出通过管道传输到less(或运行less filename.txt)。
  2. /向前搜索(或?向后搜索)。
  3. 输入您的搜索词(默认为正则表达式,或按Ctrl-r先切换为使用简单字符串)。按Enter
  4. 搜索词将突出显示。按n移动到下一个事件,或N移动到上一个事件。
  5. q退出。
于 2012-02-22T17:12:50.113 回答