LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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, 10: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 10:24 AM.
 
Old 05-05-2008, 11:59 AM   #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 12:01 PM.
 
Old 05-05-2008, 06:58 PM   #3
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 360

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

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


Reply



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

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:21 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
Open Source Consulting | Domain Registration