Stupid regex question

Josh ChaitinPollak josh at offthehill.org
Thu Mar 2 12:50:39 EST 2006


On Mar 2, 2006, at 12:09 PM, Seth Gordon wrote:

> Josh ChaitinPollak wrote:
>> I'm trying to detect the presence of a word on a line using sed.  
>> Is  there a way to say something like, 'if the match fails, don't  
>> return  anything'? For example:
>> pardsbane $ echo "this is a -p param test" | sed -e 's/.*\-p \([a- 
>> z]* \).*/\1/'
>> param
>> pardsbane $ echo "this is a param test" | sed -e 's/.*\-p \([a-z]*  
>> \).*/\1/'
>> this is a param test
>
> $ echo "this is a param test" | sed -e 's/.*\-p \([a-z]* \).*/\1/ ;  
> t ; d' | sed -e 's/^\-p //'
> $ echo "this is a -p param test" | sed -e 's/.*\-p \([a-z]* \).*/ 
> \1/ ; t ; d' | sed -e 's/^\-p //'
> param
>
> ("t" means "if the last regexp matched, branch to the end of the  
> script"; "d" means "delete the whole pattern space")

Awesome, thanks. What is the trailing s/^\-p //' for? It doesn't seem  
to be needed, -p is never returned from the first sed.



More information about the Discuss mailing list