oHo
oHo

Reputation: 54571

sed pattern ending with a specific character or the end of line

I use grep v2.5.1 and I want to colorize the filename within the grep output.

I could use another grep command with pattern /[^/:]*\(:\|$\):

grep --color=always something */*    | grep --color '/[^/:]*\(:\|$\)'

and this same pattern also works to list files:

grep --color=always something */* -l | grep --color '/[^/:]*\(:\|$\)'

But I would prefer a sed command, and I do not know how to translate \(:\|$\) in sed :-(

For instance:

echo 'dir/file: xxxx' | sed 's|/\([^/:]*\)(:|$)|/\o033[1;35m\1\o033[0m\2|'

FYI, my complete function in ~/.bashrc

gg() {
    find . -name .svn -prune -o -type f '(' -name '*.java' -o -name '*.h' -o -name '*.cpp' -o -name 'Make*' -o -name '*.sh' ')' -print0 |
    xargs -0 grep --color=always "$@" |
    sed 's|/\([^/:]*\)(:|$)|/\o033[1;35m\1\o033[0m\2|'
}

Upvotes: 0

Views: 306

Answers (1)

oHo
oHo

Reputation: 54571

After trying some other possibilities I finally found:
grep pattern is same as sed pattern for this purpose

And my complete function is:

gg ()
{
   find . -path '*/.svn' -prune -o -type f '(' -name '*.java' -o -name '*.h' -o -name '*.hpp' -o -name '*.hxx' -o -name '*.cpp' -o -name '*.cxx' -o -name '*.c' -o -name '[Mm]akefi*[^~]' -o -name '*.sh' -o -iname '*.xml' ')' -exec grep --color=always "$@" '{}' '+' | 
   sed -u 's_\(/\|^\)\([^/:]*\)\(:\|$\)_\1\o033[1;37m\2\o033[0m\3_'
}

I am still open for any comments, suggestions, improvements, contributions...
cheers ;-)

Upvotes: 1

Related Questions