How to grep without leading whitespaces

command linegrepsedtext processing

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

Best Answer

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

Related Question