LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   can't get non-greedy matching to work in sed (https://www.linuxquestions.org/questions/linux-newbie-8/cant-get-non-greedy-matching-to-work-in-sed-640049/)

lumix 05-05-2008 10:18 AM

can't get non-greedy matching to work in sed
 
I want to do something like:

Code:

echo "/1_one/2_two/3_three.xyz" | sed 's/[/].*$/$$$/'
except that I only want to replace "3_three.xyz" and not the whole string, which the above will do. I've tried:

Code:

sed 's/[/].*?$/$$$/'
and tried moving the "?" into different locations, and every permutation of that and parentheses as well. No luck.

How can I do this (with sed)?

Thanks.

berbae 05-05-2008 11:59 AM

Is that an answer :
echo $(dirname "/1_one/2_two/3_three.xyz")'/$$$'

Kenhelm 05-05-2008 06:58 PM

With sed:-
Code:

echo "/1_one/2_two/3_three.xyz" | sed 's|\(/.*/\).*|\1$$$|'

Tinkster 05-05-2008 07:12 PM

And another sed-version :}
Code:

echo "/1_one/2_two/3_three.xyz" | sed -r 's|(/.+/)[^/]+$|\1$$$|'
/1_one/2_two/$$$



All times are GMT -5. The time now is 07:25 PM.