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?
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.
Can it be automated? into a script
Anyway you can give an example of the syntax
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
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" ???????
ahh now I see correct syntax is
sed -i "s/text/changedtext/g" filename
Thank you for all your help
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
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
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
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
That one did not error out at all. However the output from it was