DebianThis forum is for the discussion of Debian Linux.
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'm trying to write a script which among others has to replace string A in file F with string B. Despite reading the sed man page and some googled stuff, I haven't managed to do this. First, I tried the following command:
sed -e 's/A/B/w F' F
but this simply erases everything from F.
Question 1: why is this?
Then I did the following:
sed -e 's/A/B/g' F >> F.new | rm -f F | mv F.new F
This does what I want (replace A with B in F) but by brute force.
Question 2: Surely there must be a "right" way of doing this. What is it?
Well, give an actual example of what it is you're trying to do. It might be something in the string you're trying to replace and just not escaping out of properly.
I've realised that I was using an old version of sed which didn't allow the -i option. I've now got a new version and the command
sed -i 's/old/new/g' filename
does exactly what I want, namely replace old with new in file "filename".
Tibob,
Thanks for the perl tip. When I saw the command line I remembered that I actually used it in one of my first scritps, but then forgot about it. Thanks, umk
I want to replace a string in a file, the problem is that the replacement is a variable, and this doesn't work. I could not get it working with sed nor with perl.
Can anyone help me?
----------------------------------------------------------
perl -pi'.bak' -e 's/ABC/$VAR/g' file.txt
----------------------------------------------------------
sed 's/ABC/$VAR/' file.txt
----------------------------------------------------------
--> it doesnt work with $VAR, it doesn't take the variable inside, it replaces "ABC" with the string "$VAR", and this is not what I want.
Thanks a lot!
Marcel
Quote:
Originally Posted by tibob
Hi,
the problem is, you can't use sed to replace in a file, you have to use a temporary file for this.
Somenone could help me finding the solution, thanks RedHat Mailinglist!
----------------------------------------------------------
sed -i "s/ABC/$VAR/" file.txt
sed -i 's/ABC/'$VAR'/' file.txt
perl -pi'.bak' -e "s/ABC/$VAR/g" file.txt
----------------------------------------------------------
Marcel
Quote:
Originally Posted by Marcel Fritzenwallne
Hi,
I want to replace a string in a file, the problem is that the replacement is a variable, and this doesn't work. I could not get it working with sed nor with perl.
Can anyone help me?
----------------------------------------------------------
perl -pi'.bak' -e 's/ABC/$VAR/g' file.txt
----------------------------------------------------------
sed 's/ABC/$VAR/' file.txt
----------------------------------------------------------
--> it doesnt work with $VAR, it doesn't take the variable inside, it replaces "ABC" with the string "$VAR", and this is not what I want.
It's not generally recommended to resurrect old threads, unless you have something important to add to the discussion. Although I suppose you could argue that this qualifies, just barely.
The big issue about using variables in sed is really the same as in many other situations. You have to protect parts of the line that contain spaces or other reserved characters from the shell, while still allowing the variable to expand. In this case, we just need to ensure that the sed expression is passed to the command as a single argument when it is run, with the variable substituted.
The simplest way is to just double-quote the whole thing. Double-quotes escape everything except for "$,`,\" (and "!" history substitution in interactive shells). This allows variable and command substitutions to expand and certain backslash escapes to work, while protecting everything else. So as long as your expression doesn't contain any of the above characters except that of the variable, you can use this:
Code:
sed -i "s/ABC/$VAR/g" file.txt
If there are other reserved characters that need protecting, then you have to hard-quote (or otherwise escape) the parts that need protecting, and un-quote the variable, such as shwetha sagar just posted. But there's a drawback to this. If the string in the variable happens to contain whitespace, then the resulting expression would be word-split into multiple arguments after the substitution was made, and the resulting command would be broken. To guard against that, you should also double-quote the variable to protect the contents.
Let's imagine that there's a "$" in the middle of the matching expression, and a space inside the variable, for example:
Code:
sed -i 's/AB$CD/'"$VAR"'/g' file.txt
Notice how this is getting kind of hard to read. Double-quoting is generally better. Remember you can still use backslash escapes!
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.