5

I am using a bash script. When I launch my script, how can I highlight specific lines on the output? For example, all lines with "Enable" should be green, all lines with "Disable" in red and all others yellow. Like this:

When I run it, I use:

YELLOW="\e[33m"
GREEN="\e[92m"
RED="\e[31m"
printf "${YELLOW}"
./config.sh --enable 1 2 3 4 5 --disable 6 7 8 9

With this, the output is yellow. That's OK, but how can I use different colors on enable and disable lines?

The output looks like:

Enable 1
Enable 2
Enable 3
Enable 4
Enable 5
Disable 6
Disable 7
Disable 8
Disable 9
aaaaaaaaaaa
bbbbbbbbbbb
ccccccccccc
ddddddddddd

And then, after all lines with enable are in green and disable are in red, from aaaaaaaaa on, everything after that should be in the default yellow color. How can I do this?

2

2 Answers 2

6

If you insist on printf you can do it like this:

highlight() {
  YELLOW="\e[;33m"
  GREEN="\e[;92m"
  RED="\e[;31m"
  while read -r; do
    case "$REPLY" in
      *Enable*)
        printf "$GREEN%s$YELLOW\n" "$REPLY"
        ;;
      *Disable*)
        printf "$RED%s$YELLOW\n" "$REPLY"
        ;;
      \[????\]*)
        printf "$YELLOW%s\e[m%s\n" "${REPLY:0:6}" "${REPLY:6}"
        ;;
      *)
        printf "$YELLOW%s\n" "$REPLY"
        ;;
    esac
    printf "$YELLOW"
  done
}

# ./config.sh --enable 1 2 3 4 5 --disable 6 7 8 9 |& highlight
highlight < <(setsid ./config.sh --enable 1 2 3 4 5 --disable 6 7 8 9 2>&1 </dev/null)

export CLICOLOR=0
highlight < <(setsid cmake --build ./path/to/build 2>&1 </dev/null)

example output

This just reads each line and adds the correct coloring before printing it. The |& captures stderr as well. @rusty's answer would be more general but it uses sed. The resets, the $YELLOW at the end, and the setsid are many workarounds for some strange behavior from the ./config.sh script.

1
1

it worked for the percentage, but anything else was in grey. but i have managed to get it work for cmake, based on your advice:

highlight() {
  YELLOW="\e[;93m"
  GREEN1="\e[;32m"
  GREEN2="\e[;92m"
  MAGENTA="\e[;95m"
  STOP="\e[0m"
  while read -r; do
    case "$REPLY" in
      *NEON*)
        printf "$GREEN2%s$YELLOW\n" "$REPLY"
        ;;
      *Scanning*)
        printf "$MAGENTA%s$YELLOW\n" "$REPLY"
        ;;
      \[????\]*Building*)
        printf "$YELLOW%s\e[$GREEN1%s\n" "${REPLY:0:6}" "${REPLY:6}"
        ;;
      \[????\]*Linking*)
        printf "$YELLOW%s\e[$GREEN2%s\n" "${REPLY:0:6}" "${REPLY:6}"
        ;;
      *)
        printf "$YELLOW%s\n" "$REPLY"
        ;;
    esac
done
  printf "$STOP"
}

with this, cmake has all the default colors via printf. the percentage, which default is grey, now has yellow and the built line after the percentage ist also in yellow, which also is default in grey. anything else of the cmake output, has the colors, which cmake also have without printf. thanx for your help :)

the other one, with enable and disable, was for something else. not for cmake.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .