ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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 would first ask how you arrived at the expression you are using? Why does it include "" in the replacement string if you do not want them in the replacement text? And why the curly braces?
And by extension, what happens if you remove those characters, have you tried to do that?
I don't believe '~' is a special character. At least not when using regular expressions with sed. It is in the Bash shell. It expands the user's home directory. So it doesn't need to be escaped with sed.
FWIW sed -i 's/~/E;/g' file.txt worked for me
Last edited by Mechanikx; 02-04-2021 at 05:56 PM.
Reason: Syntax
Just to close this out, curly brackets are used to group commands - typically when a selection criteria is met; say particular line number or regex match. It is a standard, if not everyday, usage in sed.
I'd like to replace "~" with "E;" in the target file (without quotations).
Ah! I tripped over the forum-mangled post myself...
You don't need to escape the tilde, '~' for the regular expression, but you do need to escape the ampersand, '&' because it is special, and the semi-colon to hide it from the shell (i.e. for a different reason than the &).
The curly braces are unnecessary, but I would suggest single quotes around the whole expression in which case there is no need to additionally escape the tilde and semi-colon as the shell does not see them:
You don't need to escape the tilde, '~' for the regular expression, but you do need to escape the ampersand, '&' because it is special,
To clarify further, in Sed replacement strings, ampersand represents "the matched text" - i.e. it's the same as \0 (back-reference zero), and thus needs to be escaped to provide a literal ampersand character.
This doesn't apply in the match pattern - ampersands do not need to be escaped there.
(Aside: Some regex engines have a concept of a union operator for combining multiple character classes, and use a double ampersand for this; sed is not one of those.)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.