Replacing the text in same file using SED command
I have a String like "A.words=Ajay,Anil" in file A.And it contains a lot of other information also.
I wanted to replace "Ajay,Anil" with "Vijay,Vinay" with sed command with using existing file only(not using another file) Can anyone help on it. |
If you understand sed syntax in the least little bit, this should be very easy. Syntax:
sed 's/OLD STRING/NEW STRING/' That's the basic syntax. To operate on files in-place (i.e. not use a new file) you need the -i option to sed. Please put together a sed command to do the job you want to do, and we shall comment on it and/or provide feedback if it does not work as expected. :) P.S.: Code:
man sed |
Code:
sed -i 's/Ajay,Anil/vijay,vinay/g' filename |
These values are coming dynamically in file(Ajay,Anil: vijay,vinay).
sed '/^A.words=$/s/=$/='$temp'/' $filename what's wrong with the above script.Here temp contains (vinay,vijay).. |
I don't understand what you're doing there in post#4 - please observe the syntax shown by me and vinaytp.
However post#3 shows precisely the command you would need, to replace static instances of your string. If you want to use variables, I like to do it this way: Code:
sed "s/$OLD_VAR/$NEWVAR/g" file |
First of all, you might need to change '$temp' to "$temp".
|
First we need to find the string of "A.words" in file at starting the line, after equals(=) update with the new string.
|
I have tried with double codes also.not working..
|
Quote:
Code:
sed -i "s/\(^A.words=\).*/\1$NEWSTRING/" file EDIT: If you want to replace every occurrence, add the "g" to the end of the sed expression, inside the quotes - like I did in post #5 EDIT2: Never mind that edit - if the match is at the beginning of the line, then there can only be *one* match per line. :doh: Is this what you're getting at? :) |
Yes I got the solution same as above..
|
All times are GMT -5. The time now is 11:56 AM. |