开发者

grep on colored lines

I wrote a simple PHP shell script which parses files and outputs certain element.

It generates lo开发者_开发知识库ts 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?


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜