| | |
Replacing text
![]() |
•
•
Join Date: May 2005
Posts: 215
Reputation:
Solved Threads: 16
for this example I will take the file an an input and make a new file as its output
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
it would be very easy to automate it into a script
Shell Scripting Syntax (Toggle Plain Text)
sed -i "s/string/new_string/g"
if you want to delete all instances of string you would just substiture it with nothing
Shell Scripting Syntax (Toggle Plain Text)
sed -i "s/string//g"
it would be very easy to automate it into a script
•
•
Join Date: May 2005
Posts: 215
Reputation:
Solved Threads: 16
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
a better way would be to make a new file
or this line does the same thing
this would change the original
Shell Scripting Syntax (Toggle Plain Text)
sed -i "s/username/user/g" hello
a better way would be to make a new file
Shell Scripting Syntax (Toggle Plain Text)
sed "s/username/user/g" hello > newhello
Shell Scripting Syntax (Toggle Plain Text)
cat hello | sed "s/username/user/g" > newhello
•
•
Join Date: May 2005
Posts: 215
Reputation:
Solved Threads: 16
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)
sed "s/text/sub_text/"
•
•
Join Date: May 2005
Posts: 215
Reputation:
Solved Threads: 16
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)
shane@mainbox ~ $ pwd | sed "s/\/home\/shane/\/new\/directory/" /new/directory
Last edited by shanenin; Jul 21st, 2006 at 5:58 pm.
In a perfect world exceptions would not be needed.
![]() |
Similar Threads
- Replacing text within a file using awk (Shell Scripting)
- replacing text with sed (Shell Scripting)
- want to write latin translator what language should i choose (Computer Science)
Other Threads in the Shell Scripting Forum
- Previous Thread: printing path and making it relative.
- Next Thread: i want to get output through shell script
| Thread Tools | Search this Thread |





