Visit Jeremy's Blog.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 05-05-2008, 10:18 AM   #1
Registered: Mar 2007
Distribution: Hardy (Gnome on Ubuntu 8.04) on Compaq N600c laptop
Posts: 323

Rep: Reputation: 30
can't get non-greedy matching to work in sed

I want to do something like:

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

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)?


Last edited by lumix; 05-05-2008 at 10:24 AM.
Old 05-05-2008, 11:59 AM   #2
Registered: Jul 2005
Location: France
Distribution: Arch Linux
Posts: 540

Rep: Reputation: Disabled
Is that an answer :
echo $(dirname "/1_one/2_two/")'/$$$'

Last edited by berbae; 05-05-2008 at 12:01 PM.
Old 05-05-2008, 06:58 PM   #3
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 335

Rep: Reputation: 141Reputation: 141
With sed:-
echo "/1_one/2_two/" | sed 's|\(/.*/\).*|\1$$$|'
Old 05-05-2008, 07:12 PM   #4
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
And another sed-version :}
echo "/1_one/2_two/" | sed -r 's|(/.+/)[^/]+$|\1$$$|'


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
sed or grep : delete lines containing matching text raj000 Linux - General 18 09-08-2012 09:38 AM
sed/awk for matching & substituition laikos Programming 3 11-22-2006 11:44 AM
Sed pattern matching digitalbrutus Programming 1 08-20-2006 01:37 PM
pattern matching problem in sed digitalbrutus Programming 4 08-20-2006 04:40 AM
sed matching an apostrophe dazdaz Linux - General 2 04-28-2006 05:24 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:11 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration