View previous topic :: View next topic |
Author |
Message |
MrOnion n00b
Joined: 17 Nov 2002 Posts: 24
|
Posted: Fri Apr 18, 2003 8:28 pm Post subject: Quick text replacement script? |
|
|
I have a problem with a quick script I want to create. I want to take some text in a file and replace it I must use tools that are in every distro.
So I tried:
Code: | sed -e "s/SDL/bob/" ttyut |
This had the desired effect with the output in the console
So i piped output back to the file:
Code: | sed -e "s/SDL/bob/" ttyut > ttyut |
This made ttyut a blank file.
I then tried
Code: | sed -e "s/SDL/bob/" ttyut > apple |
which worked but is less than elegant as it would need a "mv" command afterward.
Last try I had:
Code: | echo -e `sed "s/SDL/bob/" ttyut` > ttyut |
but that stripped all the newlines out.....
HELP!
thnx in advance |
|
Back to top |
|
|
quikchaos Tux's lil' helper
Joined: 29 Jan 2003 Posts: 107
|
Posted: Fri Apr 18, 2003 8:37 pm Post subject: |
|
|
Would using a '>>' instead of a '>' help keep those lines appended in there? I may be way off here. _________________ "Whether you think you can or you think you can't... you're right." -- Henry Ford |
|
Back to top |
|
|
bdufour n00b
Joined: 05 Dec 2002 Posts: 20 Location: Piscataway, NJ
|
Posted: Fri Apr 18, 2003 8:52 pm Post subject: |
|
|
What about:
Code: | cat ttyut | sed -e "s/SDL/bob/" > ttyut |
sed can be used as a filter. I think your problem is that you try to read and write from ttyut at the same time |
|
Back to top |
|
|
MrOnion n00b
Joined: 17 Nov 2002 Posts: 24
|
Posted: Fri Apr 18, 2003 8:55 pm Post subject: |
|
|
well.... erm no it didn't help too much.... HEY but thanks for the quick reply.
What it did was append the output of sed to the file so now i have the original file and then underneath i have the replaced file.
erm..... |
|
Back to top |
|
|
MrOnion n00b
Joined: 17 Nov 2002 Posts: 24
|
Posted: Fri Apr 18, 2003 9:00 pm Post subject: |
|
|
bdufour: thanks very much that works perfectly.
Onion |
|
Back to top |
|
|
wick n00b
Joined: 27 Mar 2003 Posts: 35
|
Posted: Sat Apr 19, 2003 5:45 am Post subject: |
|
|
Using the cat method is still dangerous. For your particular situation it works, but for large files it will clobber the original file before it finishes reading the input. I assume the original file is somewhat small, so it fits in the cat buffer space. prior to writing to the pipe.
Just be careful when you use this method. It's not 100% safe. Typically the best option is to write to a temp file then after the operation, move the temp file to the source, if that's what you're trying to do. |
|
Back to top |
|
|
StuBear Apprentice
Joined: 26 Feb 2003 Posts: 157 Location: Melbourne,AUSTRALIA
|
Posted: Sat Apr 19, 2003 5:57 am Post subject: |
|
|
If you are using sed > 4.0 then you can use
Code: | sed -i -e "s/SDL/bob/" ttyut |
The -i flag tells sed to write it back to the same file. No messing around with pipes or rediredcts. |
|
Back to top |
|
|
|