[NTLUG:Discuss] removing text: fighting tr and regex

Stuart Johnston saj at thecommune.net
Thu Jul 6 11:55:01 CDT 2006


Richard Geoffrion wrote:
> Victor Brilon wrote:
>> On Jul 6, 2006, at 3:03 AM, Richard Geoffrion wrote:
>>
>>   
>>> echo "jdoe at example.com (John Doe)" | sed --expression s/\(.*?\)//g
>>>     
>> You're almost there:
>> echo "jdoe at example.com (John Doe)" | sed -e s/\(.*\)//g
>>
>> To badly paraphrase "The Princess Bride": You keep using the ?  
>> qualifier, I don't think it does what you think it does :)
>>
>>   
> Actually... I think I DO understand what the "?" does.. it prevents the 
> following from happening....
> 
> $ echo "jdoe at example.com (John Doe), jane at example.com (Jane Doe)" | sed 
> s/\(.*\)//g
> jdoe at example.com
> 
> Without the ?, the expression is GREEDY and takes more than I want..... 
> but obviously the expression WITH the "?" still doesn't work at the bash 
> prompt. :(
> 
> The PERL statement DOES work with the multiple parenths "( )" though.... 
> I just wish I understood why it doesn't work in bash/sed/tr.

As far as I can tell, sed does not support the non-greedy qualifier. 
Google suggests an alternate solution:

echo "jdoe at example.com (John Doe), jane at example.com (Jane Doe)"  \
| sed -e 's/([^)]*)//g'

(BTW, Perl or perl - never PERL (unless you were just capping for emphasis))



More information about the Discuss mailing list