PDA

View Full Version : [SOLVED] sed: Wildcard is Eating Too Much



Penguin Guy
June 14th, 2009, 03:08 PM
I'm trying to remove all comments from a piece of 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 (http://ubuntuforums.org/member.php?u=804725)):

~ $ echo "code # comments # more comments" | sed 's_^\([^#]*\).*$_\1_'
code
or

~ $ echo "code # comments # more comments" | perl -p -e 's/^(.*?)#.*$/\1/'
code

MadCow108
June 14th, 2009, 03:35 PM
you could use this:
echo "code # comments # more comments" | sed 's_^\([^#]*\).*$_\1_'

alternativly you could use perl:
echo "code # comments # more comments" | perl -p -e 's/^(.*?)#.*$/\1/'

where .*? means non greedy matching
http://docstore.mik.ua/orelly/perl/cookbook/ch06_16.htm

Penguin Guy
June 14th, 2009, 07:01 PM
you could use this:
echo "code # comments # more comments" | sed 's_^\([^#]*\).*$_\1_'

alternativly you could use perl:
echo "code # comments # more comments" | perl -p -e 's/^(.*?)#.*$/\1/'

where .*? means non greedy matching
http://docstore.mik.ua/orelly/perl/cookbook/ch06_16.htm
Thanks, both solutions work perfectly!