LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 02-01-2010, 02:11 PM   #1
mrodmac
LQ Newbie
 
Registered: Mar 2006
Location: Pennsylvania, USA
Distribution: Gentoo
Posts: 6

Rep: Reputation: 0
sed - use sed to replace multiple instances from "x" to "y" in a line


Greetings

I have a line of text with multiple web links in the line. I'd like to replace the actual links with the text "<web-link>" so I don't accidently hit them while reading on my iphone. I've tried many versions of the following sed command, sed 's/(http.*)/<web_link>/g', but it simply replaces everything between the first instance of "(http" and the last instance of ")" with <web_link>, or does nothing at all.

ex.)
This line has a link to a web page (http://www.webpage.com/file.html ) then some more text (extra text) and then another link (http://www.nextwebpage.com.index.html ) to a website.

$ echo "This line has a link to a web page (http://www.webpage.com/file.html ) then some more text (extra text) and then another link (http://www.nextwebpage.com.index.html ) to a website." | sed 's/(http.*)/<web_link>/g'

what I get is:
This line has a link to a web page <web_link> to a website.

what I'd like is:
This line has a link to a web page <web_link> then some more text (extra text) and then another link <web_link> to a website.

What am I doing wrong with my sed command?
 
Old 02-01-2010, 02:54 PM   #2
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 111Reputation: 111
Regex's are greedy, they will match as much as possible. In order to make it non-greedy, try
Code:
sed 's/(http.[^)]*)/<web_link>/g'
Note how I use the "everything but )" character class rather than "everything"
 
Old 02-02-2010, 09:22 AM   #3
mrodmac
LQ Newbie
 
Registered: Mar 2006
Location: Pennsylvania, USA
Distribution: Gentoo
Posts: 6

Original Poster
Rep: Reputation: 0
tuxdev,

Thanks for the reply. It works great, altho I'm still not sure why. I'm not even sure what question to ask. Let me take a stab. So yours is saying "everything from '(http' thru to the 'first instance of )', then repeat for 'all instances of )'"?

Thanks again,
Rod
 
Old 02-02-2010, 09:57 AM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
No....his code says:
find a literal "(http", followed by any character, followed by any number of characters that are NOT ")"

replace all instances with whatever is between the last two slashes.
 
Old 02-02-2010, 11:37 AM   #5
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 111Reputation: 111
oops, that '.' was a bit of a typo.. that ultimately didn't really change much, but still..

As pixellany said, (http[^)]*) means the literal "(http" followed by zero or more characters that are not ), followed by a )
 
  


Reply

Tags
sed


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 On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
?? sed: how to use "1 s/" with -i to replace pattern only once? GrapefruiTgirl Programming 27 01-01-2010 05:39 AM
How can sed (or any other such utility) replace `x\"y' with `x\\\"y'? lumix Linux - Newbie 1 05-13-2008 08:05 PM
Replacing "function(x)" with "x" using sed/awk/smth Griffon26 Linux - General 3 11-22-2006 10:47 AM
[sed] "Advanced" sed question(s) G00fy Programming 2 03-20-2006 12:34 AM
can I replace text with the result of "wc" using sed? BrianK Linux - General 1 04-21-2004 01:15 PM


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

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration