I want to replace a text in a file eg (old_text) with another text eg (new_text) . new_text is a variable in my shell script. few methods that i tried ..
but this doesnt work
cat $line".sh" sed -e 's/abc/ABC/g' $line".sh"
sed 's/ordprg/new_string/g' $line".sh" > $line".sh"
I want to replace a text in a file eg (old_text) with another text eg (new_text) . new_text is a variable in my shell script. few methods that i tried ..
but this doesnt work
cat $line".sh" sed -e 's/abc/ABC/g' $line".sh"
sed 's/ordprg/new_string/g' $line".sh" > $line".sh"
Could some one can help me on this?
You don't need cat to display the content of `$line".sh"' to sed, in order to substitute some text.
sed 's/ordprg/new_string/g' $line".sh" > $line".sh" redirecting to the same file you have opened, is the best way of corrupting your file or at best getting a empty file.
You need to redirect the output to another file and then rename it to your original filename. If you use the GNU sed, the -i option will do that for you automatically behind the scene.
sed 's/old_string/new_string/g' "$filename" > temp_file After that rename: mv temp_file "$filename"
Just a suggestion. If you're using Gnu Sed, you can use the -i option and it will do the changes inline (basically taking care of writing to a tmp file and then copying back, which Aia had suggested. As Aia noted, it's never a good idea to overwrite your original file with output from that same file)
That would boil your command line down to:
Quote ...
sed -i 's/old_string/new_string/g' "$filename"
That makes things easier to type Even so, whether you have the option or not, redirecting your sed output to a file and then copying back is the best method. Especially if you're not sure of the outcome, in which case you should just write your statement and see what you see on STDOUT or redirect into a temp file and look it over to see if it's doing exactly what you want/need.
Thanks a lot. temp_file works fine. but now i have a different problem . when i read a text from a file and create a new file using that text two small letters disappearing from the text and that leads to an invalid file path . two letters are "e"and "n". while reading from the file it fails to read these two letters . could you please tell me possible reason for this . is it taking those letters as some delimiter or something ?
Pre requesties
1)template.txt in the same path contains a text "ordprg"and a blank line
2)filenames.sh in the same path contains two lines
"ABCDEFJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrtuvwxyz"
Following program will eliminate "e" and "n"
I think your problem has to do with the "echo" that you're using (shell built-in vs. the binary). I'd guess that on this line, the echo doesn't accept the -e and -n arguments and is using them, literally, as IFS characters, which would result in your losing them:
Thats Great. I am obliged to you . This code works fine . I eliminated -en . hope it will not be harmful for me in the future.
RGDS jinsonsanik@yahoo.com
I think your problem has to do with the "echo" that you're using (shell built-in vs. the binary). I'd guess that on this line, the echo doesn't accept the -e and -n arguments and is using them, literally, as IFS characters, which would result in your losing them:
Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.
This thread is more than three months old
No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.