I'm trying to remove all comments from a piece of code:
Code:
~ $ echo "code # comments # more comments" | sed 's_^\(.*\)#.*$_\1_
code # comments
As you can see the first wildcard .* is eating as much text as it possibly can, all the way up to the last #. How can I make it eat the bare minimum, only up to the first #?
If this is not possible, then is there some way to process sed backwards so that the last wildcard gets processed first?
Two ways of solving the problem (thanks to MadCow108):
Code:
~ $ echo "code # comments # more comments" | sed 's_^\([^#]*\).*$_\1_'
code
or
Code:
~ $ echo "code # comments # more comments" | perl -p -e 's/^(.*?)#.*$/\1/'
code
Bookmarks