Michał Kawiecki
Michał Kawiecki

Reputation: 385

How to color shell output in runtime

When I do svn up i get ~100 lines output in average. Plus there are X-teen externals that update for ~30s total (they pop-up one after another every 2-3s).

I thought of coloring (maybe transforming) this output so that I can see it more clearly.

I know I can use sed to do that but it takes in a nasty-formatted regex - lots of escaping chars.
perl on the other hand takes much cleaner regex but it waits for entire input before printing output - I get the 30s of nothing and BAM entire output appears at once.

up.sh

#!/bin/bash

svn up $@ \
    | grep -vE "^\s*$|revision" \
    | ${arhbin}/coloring/svn.sh \

${arhbin}/coloring/color_definitions.sh

#!/bin/bash
source ${arhbin}/coloring/color_definitions.sh

cat \
    | perl -pe 's/(^ *A.*$)/'$GREEN'\1'$NORMAL'/igs' \
    | perl -pe 's/(^ *D.*$)/'$RED'\1'$NORMAL'/igs' \
    | perl -pe 's/(^ *C.*$)/'$RED_BG'\1'$NORMAL'/igs' \
    | perl -pe 's/(^ *[?].*$)/'$BLUE'\1'$NORMAL'/igs' \
    | perl -pe 's/(^ *G.*$)/'$BLUE'\1'$NORMAL'/igs' \

How can I color the output of a command in runtime using Perl/Python like regex?

Upvotes: 1

Views: 79

Answers (1)

hek2mgl
hek2mgl

Reputation: 158100

On Linux you can use stdbuf to adjust io buffering. Like this:

stdbuf -oL svn up "$@" | perl ... 

Upvotes: 1

Related Questions