LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Replacing the text in same file using SED command (https://www.linuxquestions.org/questions/linux-general-1/replacing-the-text-in-same-file-using-sed-command-837405/)

mariakumar 10-11-2010 08:43 AM

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.

GrapefruiTgirl 10-11-2010 08:47 AM

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

vinaytp 10-11-2010 08:53 AM

Code:

sed -i 's/Ajay,Anil/vijay,vinay/g' filename

mariakumar 10-11-2010 09:02 AM

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)..

GrapefruiTgirl 10-11-2010 09:09 AM

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
Note that I used "double" quotes around the expression. This allows the variables to be properly expanded, without messing around with a lot of escape characters. But keep in mind, if there are special characters inside your strings you're working with (the LHS and RHS in the sed expression) then you may still need more work. For simple alphanumeric and comma characters, you should have no troubles.

sycamorex 10-11-2010 09:10 AM

First of all, you might need to change '$temp' to "$temp".

mariakumar 10-11-2010 09:13 AM

First we need to find the string of "A.words" in file at starting the line, after equals(=) update with the new string.

mariakumar 10-11-2010 09:19 AM

I have tried with double codes also.not working..

GrapefruiTgirl 10-11-2010 10:18 AM

Quote:

Originally Posted by mariakumar (Post 4123929)
First we need to find the string of "A.words" in file at starting the line, after equals(=) update with the new string.

Perhaps you mean something like:
Code:

sed -i "s/\(^A.words=\).*/\1$NEWSTRING/" file
This looks for the literal string "A.words=" at the start of the line, and replaces it with the same thing (that's why the \1 is there on the Right Hand Side), and the $NEWSTRING variable is put after the "=" sign.

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? :)

mariakumar 10-12-2010 12:20 AM

Yes I got the solution same as above..


All times are GMT -5. The time now is 11:56 AM.