Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I'm greping through a large codebase, and leading whitespaces and tabulation seem to be quite annoying. Is there any way to get rid of it?

grep -R "something" ./

E.g, instead of:

foo/bar.cpp:                       qwertyuiosomethingoi
foo/bar/baz.h:                          43rfsgsomethingdrfg
bar/bar.cpp:            1234edwssomethingczd

I want to get something like:

foo/bar.cpp: qwertyuiosomethingoi
foo/bar/baz.h: 43rfsgdsomethingrfg
bar/bar.cpp: 1234edwssomethingczd

Or better:

foo/bar.cpp:   qwertyuisomethingooi
foo/bar/baz.h: 43rfsgdrsomethingfg
bar/bar.cpp:   1234edwssomethingczd
share|improve this question
    
Get rid of it where? In the output? In the search pattern? –  Ignacio Vazquez-Abrams Sep 9 '11 at 20:57
    
@Ignacio, in output. Updated question –  Andrew Sep 9 '11 at 20:58
add comment

3 Answers

up vote 3 down vote accepted

Create test files

echo -e "\t   foo-somethingfoo" >something.foo
echo "    bar-bar-somethingbar" >something.bar_bar
echo "baz-baz-baz-somethingbaz" >something.baz_baz_baz
echo "  spaces    something  s" >something.spaces

produce full glorious colour :)

grep --colour=always "something" something.* | 
 sed -re  's/^([^:]+):(\x1b\[m\x1b\[K)[[:space:]]*(.*)/\1\x01\2\3/' |
   column -s $'\x01' -t

output (run it to get the colour).

something.bar_bar      bar-bar-somethingbar
something.baz_baz_baz  baz-baz-baz-somethingbaz
something.foo          foo-somethingfoo
something.spaces       spaces    something  s

Tested in gnome-terminal, konsole, terminator, xterm

share|improve this answer
    
good work! a little problem, though, you forget to match \t characters –  Andrew Sep 9 '11 at 23:42
    
\t ? ... It's not using \t for a seperator, it is using $'\x01' (hex 01)... or do you mean something else? –  Peter.O Sep 9 '11 at 23:57
    
I mean there may be leading tabulations \t as well as leading whitespaces \s –  Andrew Sep 10 '11 at 0:07
    
... fixed. Changed ​` ` ​​ to [[:space:]] ... If you want to only consider TAB and SPACE, and not all whitespace, use this instead: [ \t] –  Peter.O Jun 15 '12 at 1:04
add comment

Assuming you're looking for pattern re (a basic regular expression) in one file, and you'd like to strip leading whitespace from all matching lines:

sed -n -e 's/^[ \t]*//' -e '/re/p' thefile.c

(actually, this strips all leading whitespaces first, and then looks for the pattern, but the result is the same)

To post-process the grep output instead (as in your edited question):

grep re * | sed 's/:[ \t]*/: /'
share|improve this answer
    
Thanks, the last snippet works fine. Is there any way to preserve output color? –  Andrew Sep 9 '11 at 21:17
    
Colour? Call me old fashioned, but my terminal is strictly black and orange... (that's an "I don't know"). –  Kusalananda Sep 9 '11 at 21:20
1  
Use --color=always (assuming GNU grep) on the grep call. The sed call doesn't remove the colors, it's grep itself that doesn't use color when the output doesn't go to a terminal (with the default of --color=auto). "always" forces it to, well, always use color. –  Jürgen A. Erhard Sep 9 '11 at 21:37
    
@KAK: "Colour?" No, color! ;-) –  Jürgen A. Erhard Sep 9 '11 at 21:40
    
@Jurgen, thank you, but with --color=always this regexp doesn't work :/ –  Andrew Sep 9 '11 at 21:50
show 2 more comments

You can just eliminate them using sed

grep blah filename.foo | sed -e 's/^[ \t]*//'

That will remove the leading whitespaces from the output

share|improve this answer
    
This won't have any effect, since there are no spaces at the start of any line in the output. –  greenmang0 Sep 10 '11 at 8:14
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.