LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-10-2010, 10:36 AM   #1
crypted
Member
 
Registered: Jan 2004
Posts: 59

Rep: Reputation: 15
replacing text with an http link with sed?


Been at this all morning and fail on every attempt.

I have a file called sitemapindex.xml

Its contents just are names of the other sitemaps. Google gets pissy because it isn't in a http:// format.

So, I need "<loc>sitemap-c1mediawiki-............</loc>" to be replaced with "<loc>http://domain.tld/sitemap-c1mediawiki-............</loc>".

Here's the expression I've been trying:

find /path/to/site/sitemap-index-c1mediawiki* -type f | xargs sed -i 's/[<loc>sitemap]/[<loc>http://domain.tld/]/'

It fails.

I've tried with and without brackets.

Thoughts?
 
Old 12-10-2010, 11:44 AM   #2
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Try this:
Code:
sed -r 's|(<loc>)(sitemap)|\1http://domain.tld/\2|'
If you are satisfied with the result you can add the -i option.
BTW, you do not need to pipe to xargs. You can use find's -exec option instead:
Code:
find /path/to/dir/ -type f -exec sed -ri '...' '{}' +
 
Old 12-10-2010, 11:45 AM   #3
thesnow
Member
 
Registered: Nov 2010
Location: Minneapolis, MN
Distribution: Ubuntu, Red Hat, Mint
Posts: 172

Rep: Reputation: 56
Code:
echo "<loc>sitemap-c1mediawiki-" | sed 's/<loc>/<loc>http:\/\/domain.tld\//'
<loc>http://domain.tld/sitemap-c1mediawiki-
 
Old 12-10-2010, 11:51 AM   #4
crypted
Member
 
Registered: Jan 2004
Posts: 59

Original Poster
Rep: Reputation: 15
Code:
sed: -e expression #1, char 48: invalid reference \2 on `s' command's RHS
It looked perfect with the -r option output. Removed the -r and put in -i, and it won't work.
 
Old 12-10-2010, 12:03 PM   #5
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Quote:
Originally Posted by crypted View Post
Code:
sed: -e expression #1, char 48: invalid reference \2 on `s' command's RHS
It looked perfect with the -r option output. Removed the -r and put in -i, and it won't work.
The '-r' option tells sed to use extended regular expressions. Do not remove it. I said add the '-i' option; not replace '-r' for '-i'.
Code:
sed -ri ...
Also have a look at my suggestion for the alternative 'find'.

Hope this helps.
 
  


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
Replacing the text in same file using SED command mariakumar Linux - General 9 10-12-2010 12:20 AM
[SOLVED] Sed - Replacing only text with several specific lines excluded Potatos Linux - Newbie 6 06-17-2010 11:51 PM
Replacing text in files without using sed dfresh4130 Programming 16 05-28-2009 10:13 AM
Replacing text on specific lines with sed or awk? Lantzvillian Linux - Newbie 5 10-17-2007 09:00 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

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