简体   繁体   English

在 Linux (bash) 中使用 SED 在匹配前后突出显示匹配的模式和更多字符

[英]highlight matched pattern and a few more characters before and after the match using SED in Linux (bash)

I have two sets of columns of data in a file as shown below:我在一个文件中有两组数据列,如下所示:

abc-pods-7c5576f85c-zl6g2         1/1     Running       0          5d11h             xyz-pods-fbc9fb4d9-547zd          1/1     Running       0          5d10h
abc-pods-5fb6fbcdbb-7v99v         1/1     Running       1          2d14h             xyz-pods-xc5576f85c-Xl6g2         0/1     Pending       0          5d11h
abc-pods-5fb6fbcdbb-n8hbn         0/1     Pending       2          5d10h             xyz-pods-xfb6fbcdbb-Xv99v         1/1     Running       1          2d14h
abc-pods-5fb6fbcdbb-qdtz6         1/1     Running       0          5d10h             xyz-pods-xfb6fbcdbb-X8hbn         1/1     Running       2          5d10h
abc-pods-5fb6fbcdbb-zn6pn         1/1     Running       2          5d10h             xyz-pods-xfb6fbcdbb-Xdtz6         1/1     Running       0          5d10h
def-pods-6cbf6b8f8f-2pq7t         1/1     Running       1          3d9h              xyz-pods-xfb6fbcdbb-Xn6pn         1/1     Running       2          5d10h
def-pods-6cbf6b8f8f-8zxfs         1/1     Running       0          27h               pqr-pods-xcbf6b8f8f-Xpq7t         1/1     Running       1          3d9h 
def-pods-6cbf6b8f8f-fr6kc         0/1     Terminating   0          112d29h           pqr-pods-xcbf6b8f8f-Xzxfs         0/1     Pending       0          27h  
def-pods-6cbf6b8f8f-gf7gn         1/1     Running       0          2d14h             pqr-pods-xcbf6b8f8f-Xr6kc         1/1     Running       0          29h  
def-pods-6cbf6b8f8f-hs6b7         0/1     Pending       0          10h               pqr-pods-xcbf6b8f8f-Xf7gn         1/1     Running       0          2d14h
def-pods-6cbf6b8f8f-mpmsk         1/1     Running       0          28h               pqr-pods-xcbf6b8f8f-Xs6b7         1/1     Running       0          10h  
def-pods-6cbf6b8f8f-tqplz         0/1     Terminating   0          29h               pqr-pods-xcbf6b8f8f-Xpmsk         1/1     Running       0          28h  
def-pods-6cbf6b8f8f-wk6pq         1/1     Running       0          27h               pqr-pods-xcbf6b8f8f-Xqplz         1/1     Running       0          29h  
jklmno-pods-df6885fd9-6vqhs       0/1     Terminating   0          510d10h           pqr-pods-xcbf6b8f8f-Xk6pq         1/1     Running       0          27h  
mno-pods-df6885fd9-7dcbv          1/1     Running       0          5d10h             abcjkl-pods-xf6885fd9-RvqIs       0/1     Terminating   0          5d10h
mno-pods-df6885fd9-7fh9q          1/1     Running       0          29h               jkl-pods-xf6885fd9-RdcIv          0/1     Pending       0          5d10h
mno-pods-df6885fd9-cch24          1/1     Running       1          5d10h             jkl-pods-0f6885fd9-RfhIq          1/1     Running       0          29h  
mno-pods-df6885fd9-fn7qr          1/1     Running       0          5d10h             jkl-pods-0f6885fd9-RchI4          1/1     Running       1          5d10h
mno-pods-df6885fd9-k757b          1/1     Running       0          5d10h             jkl-pods-0f6885fd9-Rn7Ir          1/1     Running       0          5d10h
mno-pods-df6885fd9-kxwdn          1/1     Running       0          5d9h              jkl-pods-0f6885fd9-R75Ib          1/1     Running       0          5d10h
mno-pods-df6885fd9-m9mq8          1/1     Running       0          5d10h             jkl-pods-0f6885fd9-RxwIn          1/1     Running       0          5d9h 
mno-pods-df6885fd9-mnlxm          0/1     Pending       0          12h               jkl-pods-0f6885fd9-R9mI8          1/1     Running       0          5d10h
mno-pods-df6885fd9-r6h68          1/1     Running       0          5d10h             jkl-pods-0f6885fd9-RnlIm          1/1     Running       0          12h  
mno-pods-df6885fd9-s8vxk          1/1     Running       0          5d10h             jkl-pods-0f6885fd9-R6hI8          1/1     Running       0          5d10h
mno-pods-df6885fd9-splct          1/1     Running       0          5d10h             jkl-pods-0f6885fd9-R8vIk          1/1     Running       0          5d10h
mno-pods-df6885fd9-xbhcl          1/1     Running       0          29h               jkl-pods-0f6885fd9-RplIt          0/1     Pending       0          5d10h
mno-pods-df6885fd9-zr7bv          1/1     Running       0          5d10h             jkl-pods-0f6885fd9-RbhIl          1/1     Running       0          29h  
jkl-pods-0f6885fd9-Rr7Iv          1/1     Running       0          5d10h             jkl-pods-0bc9fb4d9-R47Id          1/1     Running       0          5d10h

What I want: is to highlight (using SED) the "entire entry" where the 0/1 is matched.我想要的是:突出显示(使用 SED)匹配 0/1 的“整个条目”。

I have already achieved the following using SED:我已经使用 SED 实现了以下目标:
图 1

What I want to achieve is:我想要实现的是:
图 2

How can I highlight 31 characters before and after 0/1如何突出显示 0/1 前后的 31 个字符
(0/1 is included in the highlighting as already depicted in the 2nd image) (0/1 包含在突出显示中,如第二张图片中所示)

I used the following code to highlight 0/1:我使用以下代码突出显示 0/1:

cat /home/McFly/myPods.log | sed -ue 's/\(0\/1\)/'`printf '\033[1;31m'`'\1'`printf '\033[0m'`'/g'

I know this can be achieved using GREP.我知道这可以使用 GREP 来实现。
I NEED to achieve it using SED.我需要使用 SED 来实现它。

A small problem remains with the output |输出仍然存在一个小问题 | watch -n 1 --color ./myScript.sh gives: watch -n 1 --color ./myScript.sh给出: 图 3

Assuming the two blocks are on the same line, and that you want to add color to the background and not the text, you can try this sed假设这两个块在同一行,并且你想为背景而不是文本添加颜色,你可以试试这个sed

$ sed -E s"~[a-z]+-[^/]*0/1([^0-9]*.){2}[^ ]*~$(tput setab 1)&$(tput sgr 0)~g" /home/McFly/myPods.log

If instead, you intend to add color to the text and not the background as your current code will do, then change setab 1 to setaf 1 ie相反,如果您打算像当前代码那样为文本而不是背景添加颜色,则将setab 1更改为setaf 1

$ sed -E s"~[a-z]+-[^/]*0/1([^0-9]*.){2}[^ ]*~$(tput setaf 1)&$(tput sgr 0)~g" /home/McFly/myPods.log

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM