Need help coloring / replacing arbitrary strings with Bash and sed

I use a bash script based on the technology used here: Get color output in bash to color the output of my collections and other scripts to make reading easier. One of the steps in my build is doing a β€œgit pull”, and the git server spits out a β€œwelcome” line like this among many other outputs:

** WARNING: THIS IS A PRIVATE NETWORK. UNAUTHORIZED ACCESS IS PROHIBITED. ** Use of this system constitutes your consent to interception, monitoring, and recording for official purposes of information related to such use, including criminal investigations. 

I want to color this particular message yellow or possibly remove it from the output, leaving the rest of the output separate. I tried replacing a simple line like this:

 WelcomeMessage="WARNING" pathpat=".*" ccred=$(echo -e "\033[0;31m") ccyellow=$(echo -e "\033[0;33m") ccend=$(echo -e "\033[0m") git pull 2>&1 | sed -r -e "/$WelcomeMessage/ s%$pathpat%$ccyellow&$ccend%g" 

The first line of the welcome line is colored yellow as expected, but the remaining lines are not. I would really like to color the exact welcome line and only this line, but for many reasons this does not work:

 WelcomeMessage="** WARNING: THIS IS A PRIVATE NETWORK. UNAUTHORIZED ACCESS IS PROHIBITED. ** Use of this system constitutes your consent to interception, monitoring, and recording for official purposes of information related to such use, including criminal investigations." pathpat=".*" ccred=$(echo -e "\033[0;31m") ccyellow=$(echo -e "\033[0;33m") ccend=$(echo -e "\033[0m") git pull 2>&1 | sed -r -e "/$WelcomeMessage/ s%$pathpat%$ccyellow&$ccend%g" 

This is not an error: sed: -e expression # 1, char 78: reterx unterminated address

I looked at a couple of other questions, and I managed to avoid the asterisks (preceding them with backslashes), but I am puzzled by periods and a few lines. I would like to continue using sed to solve this problem, as it goes well with the paint solution.

Any help is appreciated. Thanks!

+4
source share
1 answer

The following will color each row from the first instance ** in the first instance of the period in yellow . which is not on the same line. This will correspond to all warning messages as written.

 NORMAL=$(tput sgr0) YELLOW=$(tput setaf 3) git pull 2>&1 | sed "/\*\*/,/\./s/.*/$YELLOW&$NORMAL/" 

Note If you want to delete the message, you can use this:

 git pull 2>&1 | sed '/\*\*/,/\./d' 
+2
source

Source: https://habr.com/ru/post/1389104/


All Articles