Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Quick text replacement script?
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
MrOnion
n00b
n00b


Joined: 17 Nov 2002
Posts: 24

PostPosted: Fri Apr 18, 2003 8:28 pm    Post subject: Quick text replacement script? Reply with quote

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
View user's profile Send private message
quikchaos
Tux's lil' helper
Tux's lil' helper


Joined: 29 Jan 2003
Posts: 107

PostPosted: Fri Apr 18, 2003 8:37 pm    Post subject: Reply with quote

Would using a '>>' instead of a '>' help keep those lines appended in there? I may be way off here. :D
_________________
"Whether you think you can or you think you can't... you're right." -- Henry Ford
Back to top
View user's profile Send private message
bdufour
n00b
n00b


Joined: 05 Dec 2002
Posts: 20
Location: Piscataway, NJ

PostPosted: Fri Apr 18, 2003 8:52 pm    Post subject: Reply with quote

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
View user's profile Send private message
MrOnion
n00b
n00b


Joined: 17 Nov 2002
Posts: 24

PostPosted: Fri Apr 18, 2003 8:55 pm    Post subject: Reply with quote

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
View user's profile Send private message
MrOnion
n00b
n00b


Joined: 17 Nov 2002
Posts: 24

PostPosted: Fri Apr 18, 2003 9:00 pm    Post subject: Reply with quote

bdufour: thanks very much that works perfectly.

Onion
Back to top
View user's profile Send private message
wick
n00b
n00b


Joined: 27 Mar 2003
Posts: 35

PostPosted: Sat Apr 19, 2003 5:45 am    Post subject: Reply with quote

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
View user's profile Send private message
StuBear
Apprentice
Apprentice


Joined: 26 Feb 2003
Posts: 157
Location: Melbourne,AUSTRALIA

PostPosted: Sat Apr 19, 2003 5:57 am    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum