Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

grep on colored lines

Tags:

linux

grep

bash

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?

like image 349
Paul Jacobse Avatar asked Jun 08 '11 11:06

Paul Jacobse


1 Answers

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

like image 189
sehe Avatar answered Nov 06 '22 03:11

sehe