LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (http://www.linuxquestions.org/questions/linux-general-1/)
-   -   sed - use sed to replace multiple instances from "x" to "y" in a line (http://www.linuxquestions.org/questions/linux-general-1/sed-use-sed-to-replace-multiple-instances-from-x-to-y-in-a-line-786266/)

mrodmac 02-01-2010 02:11 PM

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?

tuxdev 02-01-2010 02:54 PM

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"

mrodmac 02-02-2010 09:22 AM

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

pixellany 02-02-2010 09:57 AM

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.

tuxdev 02-02-2010 11:37 AM

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 )


All times are GMT -5. The time now is 06:51 AM.