Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle color codes when trying to use grep, sed, etc

I'm trying to use sed to process some output of a command that is generating colored lines (it's git diff, but I don't think that's important). I'm trying to match a '+' sign at the beginning of the line but this is being confounded by the color code, which precedes the '+'. Is there a simple way to deal this this issue, or do I need to use some complicated regular expression to match the color code.

If possible I'd like to preserve the coloring of the line.

like image 506
jonderry Avatar asked Apr 07 '11 03:04

jonderry


People also ask

How do you grep with different colors?

You can cascade greps with different colors by specifying --color=always and using the regular expression 'foo|$' to pass all lines. To update the entire line, modifying the regex is the wrong approach. Instead, use GREP_COLORS='sl=1;31' grep ... and specify sl to color the whole line.

What is sed and grep?

Grep, sed, and AWK are all standard Linux tools that are able to process text. Each of these tools can read text files line-by-line and use regular expressions to perform operations on specific parts of the file. However, each tool differs in complexity and what can be accomplished.

Can you use regex with grep?

GNU grep supports three regular expression syntaxes, Basic, Extended, and Perl-compatible. In its simplest form, when no regular expression type is given, grep interpret search patterns as basic regular expressions. To interpret the pattern as an extended regular expression, use the -E ( or --extended-regexp ) option.

How do I grep a pattern in Linux?

To find a pattern that is more than one word long, enclose the string with single or double quotation marks. The grep command can search for a string in groups of files. When it finds a pattern that matches in more than one file, it prints the name of the file, followed by a colon, then the line matching the pattern.


2 Answers

If you must have the coloring then you're going to have to do something ugly:

$ git diff --color web-app/db/schema.rb |grep '^^[\[32m+

That ^[ is actually a raw escape character (Ctrl+V ESC in bash, ASCII 27). You can use cat -v to figure out the necessary escape sequences:

$ git diff --color web-app/db/schema.rb |cat -v
^[[1mdiff --git a/web-app/db/schema.rb b/web-app/db/schema.rb^[[m
^[[1mindex 45451a2..411f6e1 100644^[[m
^[[1m--- a/web-app/db/schema.rb^[[m
^[[1m+++ b/web-app/db/schema.rb^[[m
...

This sort of thing will work fine with the GNU versions of sed, awk, ... YMMV with non-GNU versions.

An easier way would be to turn of the coloring:

$ git diff --no-color file

But you can trade pretty output for slightly ugly regular expressions.

like image 171
mu is too short Avatar answered Oct 13 '22 21:10

mu is too short


No need to deal with ugly regular expressions, actually. You can just pass the config variable to the git command you're using to preserve coloring.

git -c color.diff=always diff | cat

This works for git status too

git -c color.status=always status -sb | cat
like image 31
shime Avatar answered Oct 13 '22 21:10

shime