4

I'm using a bash script based on the technique used here: Get color output in bash to color the output of my builds and other scripts to make things easier to read. One of the steps in my build executes a "git pull" and the git server spits out a "welcome" string like this amidst a bunch of other output:

** 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'd like to color this specific message yellow or possibly delete it from the output while leaving the rest of the output alone. I've tried to replace a simple string 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 string is colored yellow as expected but the rest of the lines are not. I'd really like to color the exact welcome string and only that string but for many reasons, this doesn't 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 fails with the error: sed: -e expression #1, char 78: unterminated address regex

I've looked at a couple other questions and I was able to get the asterisks escaped (by preceding them with backslashes) but I'm baffled by the periods and multiple lines. I'd like to continue using sed to solve this problem since it integrates nicely with the colorizing solution.

Any help is appreciated. Thanks!

Community
  • 1
  • 1
Rotsiser Mho
  • 551
  • 2
  • 5
  • 19
  • Why matching everything? What you want is to replace the end and beginning. Not sure that it works, but can you try `s,^,$ccyellow,; s,$,$ccend,`? – fge Jan 03 '12 at 16:32
  • Well for one I'd like to have the option to remove the message (and only the message) from the output. I think if I matched the end and the beginning it would color the entire output yellow wouldn't it? I only want this string colored/removed. – Rotsiser Mho Jan 03 '12 at 16:35
  • OK, so, you want only `Welcome` colored? You _do_ know that some TTYs don't support ANSI colors? – fge Jan 03 '12 at 16:40
  • I only want the welcome string colored, correct. This script will only be run in bash so the support for colors is there. My real problem is just matching the string. – Rotsiser Mho Jan 03 '12 at 16:48
  • 1
    "This script will only be run in bash" <-- this is not the question. It is your TTY which determines color support (more appropriately, the TERM variable) – fge Jan 03 '12 at 16:56
  • I appreciate the information but lets say I want to remove the string or replace it with something else. That is, ignoring the color issue how can I match the string? – Rotsiser Mho Jan 04 '12 at 04:46

1 Answers1

2

The following will colorize in yellow every line from the first instance of ** to the first instance of a period . that's not on the same line. This will match the entire warning message 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'
SiegeX
  • 135,741
  • 24
  • 144
  • 154
  • Thanks so much! Quick question: how does your magic command "skip" the first period after the word "NETWORK" (or after the word "PROHIBITED")? – Rotsiser Mho Jan 05 '12 at 06:54
  • `sed` regex is "greedy" so the range from `**` to `.` will be to the last instance of a period. The answer in my description had a typo where I said 'first' instead of 'last'. Fixed – SiegeX Jan 05 '12 at 19:12
  • If you are satisfied with my answer, please select it as your chosen answer by clicking the 'check mark' next to the vote counter. This helps both you gain reputation and also others who have similar questions. Thanks and welcome to SO – SiegeX Jan 05 '12 at 19:14
  • Ah, thanks for the clarification. However I'm still confused. The next thing Git spits out after the message (assuming I just pulled) is "Already up to date.". That message isn't yellow even though a period is present. Your command works great I just don't understand it! Thanks again. – Rotsiser Mho Jan 05 '12 at 21:36
  • @RotsiserMho That is a bit odd then, can you pastebin the actual output you see, or at least a bit of text after the warning message and any stuff before it if there is any. – SiegeX Jan 06 '12 at 00:55
  • I've copied what I can and have posted it here: http://i.imgur.com/pXzfN.png There is a slight pause between when the message is displayed and the rest of the output. Perhaps Git is launching other scripts? I'm not sure why that would matter though. Thanks! – Rotsiser Mho Jan 08 '12 at 02:20
  • @RotsiserMho thanks. Could you please copy and paste it in a non-picture format such as on [pastebin.com](http://www.pastebin.com)? Feel free to alter the stuff you edited out but just make sure the edited text still shows the same behavior. Then ill just copy that text verbatim and play with it to see what's going on. – SiegeX Jan 09 '12 at 02:46
  • My apologies. I've put it here: http://pastebin.com/7SbVWPPu Another example: http://pastebin.com/w0m4Yi71 Both of the above show the warning message as yellow but the rest is normal – Rotsiser Mho Jan 09 '12 at 05:44
  • @RotsiserMho ok, so when you give `sed` a range, the behavior is such that it does not see the end range if it is on the same line as the start range. This is why it does not stop on the period after `PROHIBITED.`. If you were to place a period on the 2nd line, you *would* see the colorizing stop there. – SiegeX Jan 09 '12 at 08:04