Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's 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'm having a problem with SED and the special character "&". I have a sentence, in which the character "&" appears three times. I saved it as a string:
And I wanted to insert this string in a text file under the first occurance of the word "</declaration>", using:
sed -i '0,/<\/declaration>/s/<\/declaration>/<\/declaration>\n'"${String}"'/' file.txt
As you can see, I was already inserting an "\" in the string before each special character, but it doesn't help in case I put it in the front of the character "&", i.e. "\&".
Would somebody know what I need to do, to be able to have "&" in my string and insert it in the text file using the command SED?
PS: "InputName" and "a" are variables with the values: "output" and "0"
Note :
String='\&' single quotes and only the & characters are with backslash
and
sed "s+++" double quotes and the + sign for the s command (instead of /).
both solutions are not working... I need it to insert the "String" just after the first occurance of the word "</declaration>". And $inputName needs to be substituted by its value, so once SED inserts the sentence it needs to write "output" and NOT "$inputName" in the file.
The awk command is printing the changes on my terminal, what do I need to do, so that the changes are saved in the file? Otherwise your solution also doesn't help me...
input and output redirection are the very basics of scripting or working with the terminal.
type "man bash" , then type "/" followed by REDIRECTION. otherwise, search google for these terms.
Thanks! Now I could redirect, but the result of your command was inserting the "string" after every occurence of <declaration> in my text. Is there a way for just insert the "string" after the first occurance of </declaration>?
There was a problem with the String declaration, at the end of it, with the ctrl"'!'" part (due to the presence of single quotes).
But apart from that and the "$inputName needs to be substituted by its value" which you didn't mention in the first post, it works on my machine, ie the & appear correctly.
Here is what I get now with your new infos.
I tested the command with this file.txt :
thanks again for your post. I copied exactly what you posted, but I still get "</declaration>amp" on my machine instead of "&". It is really strange. Do you know if this could be cause because of the linux or bash version? Anyways thanks again.
Thanks! Now I could redirect, but the result of your command was inserting the "string" after every occurence of <declaration> in my text. Is there a way for just insert the "string" after the first occurance of </declaration>?
1) I realize from your first post that the end of the String should be ctrl!</label> and not, as I misunderstood, ctrl"'!'"</label>.
So in my proposed code :
ctrl'"\"'"\!"'\""'</label>'
should be replaced by :
ctrl!</label>'
2) It seems that your sed doesn't escaped the & character with the \ before it, as it does on my machine. So for you the & is interpreted laterally to mean the matching string, even with a \ before it.
I don't know exactly why, but probably differences in versions.
For infos I give the versions I used :
GNU bash, version 4.0.35(2)-release
GNU sed version 4.2.1
GNU Awk 3.1.7
3) I looked into the ghostdog74 code, and I could have it to work with these modifications :
In the string declaration, only the " inside the string are escaped with one \, and & needs four \ on my machine. No need of the single quotes, because then the $inputName is not replaced by its value.
Apart from that I get the same output with my file.txt test file as with the sed command.
Post a follow-up if you find a working solution.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.