Replacing text

Reply

Join Date: Aug 2005
Posts: 20
Reputation: chrchcol is an unknown quantity at this point 
Solved Threads: 0
chrchcol chrchcol is offline Offline
Newbie Poster

Replacing text

 
0
  #1
Aug 2nd, 2005
I have an index.html file that I need to be able to modify depending on if its secure or unsecure, can I use cat and or grep in a script to modify text in it?

Chris
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 215
Reputation: shanenin is an unknown quantity at this point 
Solved Threads: 16
shanenin shanenin is offline Offline
Posting Whiz in Training

Re: Replacing text

 
0
  #2
Aug 2nd, 2005
cat piped to grep can check to see if a line of text contains a string. the sed command is more suited for editing files. Sed is a good utility to modify text. Sed is good at deleting strings and also substituing one string for an other.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 20
Reputation: chrchcol is an unknown quantity at this point 
Solved Threads: 0
chrchcol chrchcol is offline Offline
Newbie Poster

Re: Replacing text

 
0
  #3
Aug 2nd, 2005
Can it be automated? into a script

Anyway you can give an example of the syntax
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 215
Reputation: shanenin is an unknown quantity at this point 
Solved Threads: 16
shanenin shanenin is offline Offline
Posting Whiz in Training

Re: Replacing text

 
0
  #4
Aug 2nd, 2005
for this example I will take the file an an input and make a new file as its output
Shell Scripting Syntax (Toggle Plain Text)
  1. sed -i "s/string/new_string/g"
this code will substitute all instances of 'string' with 'new_string'

if you want to delete all instances of string you would just substiture it with nothing
Shell Scripting Syntax (Toggle Plain Text)
  1. sed -i "s/string//g"

it would be very easy to automate it into a script
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 20
Reputation: chrchcol is an unknown quantity at this point 
Solved Threads: 0
chrchcol chrchcol is offline Offline
Newbie Poster

Re: Replacing text

 
0
  #5
Aug 2nd, 2005
tried it and I can't quite get it right

Lets say I created a file called "hello" With the words username and password in it.

I wanted to change username to user

would I use

sed hello -i "s/username/user/g" ???????
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 20
Reputation: chrchcol is an unknown quantity at this point 
Solved Threads: 0
chrchcol chrchcol is offline Offline
Newbie Poster

Re: Replacing text

 
0
  #6
Aug 2nd, 2005
ahh now I see correct syntax is

sed -i "s/text/changedtext/g" filename

Thank you for all your help

Chris
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 215
Reputation: shanenin is an unknown quantity at this point 
Solved Threads: 16
shanenin shanenin is offline Offline
Posting Whiz in Training

Re: Replacing text

 
0
  #7
Aug 2nd, 2005
if hello is the name of the file put it at the end. using the -i option changes your original file. You would want to keep your original unchanged, so it can be used as a template.

this would change the original
Shell Scripting Syntax (Toggle Plain Text)
  1. sed -i "s/username/user/g" hello

a better way would be to make a new file
Shell Scripting Syntax (Toggle Plain Text)
  1. sed "s/username/user/g" hello > newhello
or this line does the same thing
Shell Scripting Syntax (Toggle Plain Text)
  1. cat hello | sed "s/username/user/g" > newhello
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 215
Reputation: shanenin is an unknown quantity at this point 
Solved Threads: 16
shanenin shanenin is offline Offline
Posting Whiz in Training

Re: Replacing text

 
0
  #8
Aug 2nd, 2005
the g means global. if you do not use the g, only the first instance of the sting is changed. In you case, you would not need the g
Shell Scripting Syntax (Toggle Plain Text)
  1. sed "s/text/sub_text/"
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 20
Reputation: chrchcol is an unknown quantity at this point 
Solved Threads: 0
chrchcol chrchcol is offline Offline
Newbie Poster

Re: Replacing text

 
0
  #9
Jul 21st, 2006
Is there a way to make this exression work

pwd | sed -e "s/home/httpd/vusers/*.domain.com/web_users/chris2 /chris.domain.com/web_users/chris2/g"

Chris
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 215
Reputation: shanenin is an unknown quantity at this point 
Solved Threads: 16
shanenin shanenin is offline Offline
Posting Whiz in Training

Re: Replacing text

 
0
  #10
Jul 21st, 2006
Since the forward slash has special meaning, you need to escape it. By placing a backslash in front of the forward slash, all of the special meaning is removed(escaped). for example my home directory is /home/shane. I will do an example that works
Shell Scripting Syntax (Toggle Plain Text)
  1. shane@mainbox ~ $ pwd | sed "s/\/home\/shane/\/new\/directory/"
  2. /new/directory
Last edited by shanenin; Jul 21st, 2006 at 5:58 pm.
In a perfect world exceptions would not be needed.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Shell Scripting Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC