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? Sep 9, 2011 at 20:57
  • @Ignacio, in output. Updated question
    – Andrew
    Sep 9, 2011 at 20:58

3 Answers 3


You can just eliminate them using sed

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

That will remove the leading whitespaces from the output

  • 2
    This won't have any effect, since there are no spaces at the start of any line in the output.
    – Abhishek A
    Sep 10, 2011 at 8:14
  • Only relevant if grep is used with -h
    – kostr22
    Nov 24, 2021 at 9:26

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/^[[:blank:]]*//' -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 -e 're' -- * | sed 's/:[[:blank:]]*/: /'

The pattern [[:blank:]]* matches zero or more spaces or tabs.

If you insert a tab instead of a space after the :, you additionally get some of the nice even indentation you requested.

  • Thanks, the last snippet works fine. Is there any way to preserve output color?
    – Andrew
    Sep 9, 2011 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, 2011 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. Sep 9, 2011 at 21:37
  • @Jurgen, thank you, but with --color=always this regexp doesn't work :/
    – Andrew
    Sep 9, 2011 at 21:50
  • 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 Sep 11, 2011 at 15:55

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, 2011 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, 2011 at 23:57
  • I mean there may be leading tabulations \t as well as leading whitespaces \s
    – Andrew
    Sep 10, 2011 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, 2012 at 1:04

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.