Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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)
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.
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.
Is this what you're getting at?
Last edited by GrapefruiTgirl; 10-11-2010 at 10:26 AM.
Reason: See EDIT
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.