pipes and streams and one file
Hi,
I'm writing little Sed wrapper. It's task is to replace text. I have to override original file. so my first try was: Code:
#!/bin/bash Code:
./my_script foo bar file Code:
foo Anyway I fixed this with: Code:
#!/bin/bash The second approach works but maybe I should better use tmp file? Sorry for my poor English. And TIA for answers. |
yes it's better to use a temp file in this case. when you use > it wipes the file you are writing to and since you were reading and writing from the same file the contents were getting wiped before you did anything with them. The pipe to cat only worked because the file was small. If it had been larger than one buffered read I'd bet you see similar problems to your first attempt.
|
And in addition to the temp-files idea:
newer GNU seds (> 3) allow in-file changes. Code:
sed -i.bak s/$1/$2/g $3 Cheers, Tink |
All times are GMT -5. The time now is 12:02 AM. |