LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


Reply
  Search this Thread
Old 05-05-2008, 11:18 AM   #1
lumix
Member
 
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:

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.

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

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

Last edited by berbae; 05-05-2008 at 01:01 PM.
 
Old 05-05-2008, 07:58 PM   #3
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 333

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

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


Reply


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 10:38 AM
sed/awk for matching & substituition laikos Programming 3 11-22-2006 12:44 PM
Sed pattern matching digitalbrutus Programming 1 08-20-2006 02:37 PM
pattern matching problem in sed digitalbrutus Programming 4 08-20-2006 05:40 AM
sed matching an apostrophe dazdaz Linux - General 2 04-28-2006 06:24 PM


All times are GMT -5. The time now is 12:16 PM.

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