Paul Jacobse
Paul Jacobse

Reputation: 454

grep on colored lines

I wrote a simple PHP shell script which parses files and outputs certain element.
It generates lots of output. In different (bash) colors, green for OK, yellow for warnings, red for errors, etc.

During development I want to filter some lines out. For example all lines that contains red text.

Can I use a grep (or other) command for this?

Upvotes: 7

Views: 3585

Answers (2)

Roman Grazhdan
Roman Grazhdan

Reputation: 477

As far as I understand, you parse the input once to colorize it anyway, right? Why not 'cut out' warnings/errors in the same function? Make your script use command line options, like myscript --nowarnings

There is getopt for PHP tutorial here

I don't know any php, but something like (pseudocode):

paintred(string, show){
    match(string);
    if(show){
        print(string) in red;
    }
    else return 0;
}

Where show would depend on command line option.

This way you only parse the file once, and you give the future users an option to skip OK lines or warnings.

Upvotes: 0

sehe
sehe

Reputation: 392911

I have no idea what your input looks like, but as a proof of concept you can filter any lines in ls output that use green colour:

ls --color=always | grep '^[\[01;32m'

The lookup table for other colours can be found here: http://en.wikipedia.org/wiki/ANSI_escape_code#Colors

Hint: In case you didn't know, the ^[ part above should be entered like Ctrl-VEsc (or indeed Ctrl-VCtrl-[ on most terminals). I'm sure there will be some option to grep to make it understand \x1B instead, but I haven't found it

Upvotes: 8

Related Questions