16

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
  • 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
4

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

  • 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
6

You can just eliminate them using sed

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

That will remove the leading whitespaces from the output

  • 1
    This won't have any effect, since there are no spaces at the start of any line in the output. – Abhishek A Sep 10 '11 at 8:14
5

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]*/: /'
  • 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
  • 3
    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
  • 1
    Oh shucks, you're right. That's because there are control sequences (for the color) between the colon and the whitespace. You could put the escape sequences into the sed call (the sequence is, at least for a bog-standard vt100 emulation (xterm, screen, etc) "\033[m\033[K". I think. ;D – Jürgen A. Erhard Sep 11 '11 at 15:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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