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 09-29-2010, 11:11 AM   #1
Mike_V
Member
 
Registered: Apr 2009
Location: Boston MA
Distribution: CentOS 6.2 x86_64 GNU/Linux
Posts: 59

Rep: Reputation: 19
Replacing a string of text that includes "/"


Suppose I have a file named temp.txt

cat temp.txt
word/one/two/three

If I would like to replace "one" with "six" I would do this:

cat temp.txt | sed 's/one/six/'
word/six/two/three


Now I want to replace "one/two" with "six/seven" and these options don't work:
cat temp.txt | sed 's/one/two/six/seven/' (OFCOURSE)
cat temp.txt | sed 's/"one/two"/"six/seven"/'

I even want to be able to replace "one/two" with "six/seven/eight"


Is there a simple way do do this?

Thanks!
 
Old 09-29-2010, 11:23 AM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
'sed -i "s|one/two|six/seven|g" temp.txt'?
 
1 members found this post helpful.
Old 09-29-2010, 11:41 AM   #3
Mike_V
Member
 
Registered: Apr 2009
Location: Boston MA
Distribution: CentOS 6.2 x86_64 GNU/Linux
Posts: 59

Original Poster
Rep: Reputation: 19
unSpawn: thanks a lot, yes, that did the trick!

quick question: why did you add the "g" at the end? In my case it doesn't seem to matter if I include it or not... I'm just curious....
 
Old 09-29-2010, 11:51 AM   #4
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
Since you're working with regular expressions where your search query contains a slash, unSpawn changed to using a pipe symbol (|) as the delimiter character in sed, instead of using slashes. This makes the regex simpler, because you don't have to escape the slashes in your search/replacement text.

The 'g' makes sed continue processing the input even after it has made a replacement (to see if there is more to replace), rather than quitting after one replacement. Try doing multiple replacements on a single line, and see the difference with and without the 'g' on the end:
Code:
root@reactor: echo "one one one" | sed 's/one/two/'
two one one
root@reactor: echo "one one one" | sed 's/one/two/g'
two two two
root@reactor:
 
1 members found this post helpful.
Old 09-29-2010, 11:52 AM   #5
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
I usually want to delete all instances of something which is what "g" does (as far as I know sed). Sure you don't need to use it.
 
Old 09-29-2010, 11:57 AM   #6
Mike_V
Member
 
Registered: Apr 2009
Location: Boston MA
Distribution: CentOS 6.2 x86_64 GNU/Linux
Posts: 59

Original Poster
Rep: Reputation: 19
@GrapefruiTgirl: thanks for explaining that!
 
Old 09-29-2010, 07:21 PM   #7
kurumi
Member
 
Registered: Apr 2010
Posts: 228

Rep: Reputation: 53
Code:
$ ruby -pne 'gsub("one/two" ,"six/seven")' file
word/six/seven/three
 
  


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
How to stop bash from replacing "~/" with "/home/username"? daihard Linux - Software 8 08-22-2010 02:32 PM
Shell script: I have string "abc____def____ghi", how to make "abc def ghi" vouser Programming 8 03-09-2010 10:01 PM
Beagle: Searching for text string containing "-" character pgb205 Linux - Software 2 06-26-2008 07:28 PM
"Permission denied" and "recursive directory loop" when searching for string in files mack1e Linux - Newbie 5 06-12-2008 07:38 AM
Replacing "function(x)" with "x" using sed/awk/smth Griffon26 Linux - General 3 11-22-2006 10:47 AM

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

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