Home > Mobile >  highlight matched pattern and a few more characters before and after the match using SED in Linux (b
highlight matched pattern and a few more characters before and after the match using SED in Linux (b

Time:06-07

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.

I have already achieved the following using SED:
Image 1

What I want to achieve is:
Image 2

How can I highlight 31 characters before and after 0/1
(0/1 is included in the highlighting as already depicted in the 2nd image)

I used the following code to highlight 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.
I NEED to achieve it using SED.

A small problem remains with the output | watch -n 1 --color ./myScript.sh gives: Image 3

CodePudding user response:

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 -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 i.e

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