LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 09-12-2005, 12:03 AM   #1
Johng
Member
 
Registered: Feb 2002
Location: NZ
Distribution: Kubuntu, Mint
Posts: 408

Rep: Reputation: 31
sed


When using sed to replace text in file(s), how do you use a slash '/' as a text character? As an example, I want to replace the string "index" with "../index"

This expression works:
find -name "*.html" -exec sed -i 's/"index.html"/"..index.html"/g' {} \;

This one does not because there are more than two / in the expression:
find -name "*.html" -exec sed -i 's/"index.html"/"../index.html"/g' {} \;
 
Old 09-12-2005, 12:54 AM   #2
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 61
You could escape special characters with a \

for example:
cat file.txt
now is the time for all good men

sed -e 's/time/\.\.\/time/' file.txt
now is the ../time for all good men
 
Old 09-12-2005, 12:56 AM   #3
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
Re: sed

Quote:
Originally posted by Johng
When using sed to replace text in file(s), how do you use a slash '/' as a text character? As an example, I want to replace the string "index" with "../index"

This expression works:
find -name "*.html" -exec sed -i 's/"index.html"/"..index.html"/g' {} \;

This one does not because there are more than two / in the expression:
find -name "*.html" -exec sed -i 's/"index.html"/"../index.html"/g' {} \;
find -name "*.html" -exec sed -i 's^index.html^\.\./index.html^g' {} \;

Basically sed can accept any character that comes first
after the s as the separator. Thus, if you know that your
text will never contain a caret or a tilde you can easily
use those for seperators.


Cheers,
Tink
 
Old 09-12-2005, 12:57 AM   #4
CroMagnon
Member
 
Registered: Sep 2004
Location: New Zealand
Distribution: Debian
Posts: 900

Rep: Reputation: 33
You should 'escape' the slash by putting a backslash in front of it. e.g:

's/"index.html"/"..\/index.html"/g'

Otherwise, you can use another character as a separator in place of the forward slashes. e.g:

's%"index.html"%"../index.html"%g'
 
Old 09-12-2005, 12:58 AM   #5
CroMagnon
Member
 
Registered: Sep 2004
Location: New Zealand
Distribution: Debian
Posts: 900

Rep: Reputation: 33
Wow, three posts within three minutes of each other Guess I should use the preview button more often.
 
Old 09-12-2005, 01:12 AM   #6
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
Quote:
Originally posted by CroMagnon
Wow, three posts within three minutes of each other :)
And two of those from NZ ;}



Cheers,
Tink
 
Old 09-12-2005, 01:21 AM   #7
Johng
Member
 
Registered: Feb 2002
Location: NZ
Distribution: Kubuntu, Mint
Posts: 408

Original Poster
Rep: Reputation: 31
Thanks guys, Great!
 
Old 09-12-2005, 08:03 PM   #8
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 234Reputation: 234Reputation: 234
I use the ',' (comma) routinely for readability.
 
  


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 and escaping & in something like: echo $y | sed 's/&/_/g' prx Programming 7 02-03-2005 11:00 PM
Sed q? ky-lab_rat Linux - Software 5 10-26-2004 07:59 AM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 06:12 AM
Help with sed odious1 Linux - General 2 08-29-2003 10:52 AM
help with sed DavidPhillips Programming 3 08-11-2003 04:46 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 12:53 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