How do I grep without leading whitespaces?
You can just eliminate them using sed
grep blah filename.foo | sed -e 's/^[ \t]*//'
That will remove the leading whitespaces from the output
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.
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