in the first attempt, you need to add a pipe ('|') between the cat command and the sed command
DimaYasny
Posting Virtuoso
1,777 posts since Jan 2007
Reputation Points: 183
Solved Threads: 89
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 needcat 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"
Aia
Nearly a Posting Maven
2,392 posts since Dec 2006
Reputation Points: 2,224
Solved Threads: 218