943,847 Members | Top Members by Rank

Ad:
You are currently viewing page 1 of this multi-page discussion thread
Aug 2nd, 2005
0

Replacing text

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
chrchcol is offline Offline
22 posts
since Aug 2005
Aug 2nd, 2005
0

Re: Replacing text

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.
Reputation Points: 10
Solved Threads: 17
Posting Whiz in Training
shanenin is offline Offline
217 posts
since May 2005
Aug 2nd, 2005
0

Re: Replacing text

Can it be automated? into a script

Anyway you can give an example of the syntax
Reputation Points: 10
Solved Threads: 0
Newbie Poster
chrchcol is offline Offline
22 posts
since Aug 2005
Aug 2nd, 2005
0

Re: Replacing text

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
Reputation Points: 10
Solved Threads: 17
Posting Whiz in Training
shanenin is offline Offline
217 posts
since May 2005
Aug 2nd, 2005
0

Re: Replacing text

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" ???????
Reputation Points: 10
Solved Threads: 0
Newbie Poster
chrchcol is offline Offline
22 posts
since Aug 2005
Aug 2nd, 2005
0

Re: Replacing text

ahh now I see correct syntax is

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

Thank you for all your help

Chris
Reputation Points: 10
Solved Threads: 0
Newbie Poster
chrchcol is offline Offline
22 posts
since Aug 2005
Aug 2nd, 2005
0

Re: Replacing text

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
Reputation Points: 10
Solved Threads: 17
Posting Whiz in Training
shanenin is offline Offline
217 posts
since May 2005
Aug 2nd, 2005
0

Re: Replacing text

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/"
Reputation Points: 10
Solved Threads: 17
Posting Whiz in Training
shanenin is offline Offline
217 posts
since May 2005
Jul 21st, 2006
0

Re: Replacing text

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
chrchcol is offline Offline
22 posts
since Aug 2005
Jul 21st, 2006
0

Re: Replacing text

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.
Reputation Points: 10
Solved Threads: 17
Posting Whiz in Training
shanenin is offline Offline
217 posts
since May 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Shell Scripting Forum Timeline: printing path and making it relative.
Next Thread in Shell Scripting Forum Timeline: i want to get output through shell script





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC