954,525 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Replacing text

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

chrchcol
Newbie Poster
22 posts since Aug 2005
Reputation Points: 10
Solved Threads: 0
 

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.

shanenin
Posting Whiz in Training
217 posts since May 2005
Reputation Points: 10
Solved Threads: 17
 

Can it be automated? into a script

Anyway you can give an example of the syntax

chrchcol
Newbie Poster
22 posts since Aug 2005
Reputation Points: 10
Solved Threads: 0
 

for this example I will take the file an an input and make a new file as its output

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

sed -i "s/string//g"


it would be very easy to automate it into a script

shanenin
Posting Whiz in Training
217 posts since May 2005
Reputation Points: 10
Solved Threads: 17
 

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" ???????

chrchcol
Newbie Poster
22 posts since Aug 2005
Reputation Points: 10
Solved Threads: 0
 

ahh now I see correct syntax is

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

Thank you for all your help

Chris

chrchcol
Newbie Poster
22 posts since Aug 2005
Reputation Points: 10
Solved Threads: 0
 

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

sed -i "s/username/user/g" hello


a better way would be to make a new file

sed "s/username/user/g" hello > newhello

or this line does the same thing

cat hello | sed "s/username/user/g" > newhello
shanenin
Posting Whiz in Training
217 posts since May 2005
Reputation Points: 10
Solved Threads: 17
 

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

sed "s/text/sub_text/"
shanenin
Posting Whiz in Training
217 posts since May 2005
Reputation Points: 10
Solved Threads: 17
 

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

chrchcol
Newbie Poster
22 posts since Aug 2005
Reputation Points: 10
Solved Threads: 0
 

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

shane@mainbox ~ $ pwd | sed "s/\/home\/shane/\/new\/directory/"
/new/directory
shanenin
Posting Whiz in Training
217 posts since May 2005
Reputation Points: 10
Solved Threads: 17
 

pwd | sed "s/\/home/\/httpd/\/vusers/\/*.fileburst.com/\/web_users/\/chris2/\/chris.fileburst.com/\/web_users/\/chris2"
This is what I tried and it did not work, can you show me the flaw?

chrchcol
Newbie Poster
22 posts since Aug 2005
Reputation Points: 10
Solved Threads: 0
 

If you use code tags your post would be a little easier to read. Try this

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


I am not sure, but the "*" might not wotrk with sed. let me know if this works

shanenin
Posting Whiz in Training
217 posts since May 2005
Reputation Points: 10
Solved Threads: 17
 

That one did not error out at all. However the output from it was

/root

Chris

chrchcol
Newbie Poster
22 posts since Aug 2005
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You