LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-04-2021, 04:02 PM   #1
__John_L.
LQ Newbie
 
Registered: Feb 2021
Posts: 14

Rep: Reputation: Disabled
Escape characters in sed stream


I'm trying to use sed on the linux command line, and am running into an issue with special characters.

I'd like to replace "~" with "E;" in the target file (without quotations). So far, using this command:

sed {s/\~/\&#7E\;/g}

on this data:

blah~blah

produces:

blah~#7E;blah

Close, but no cigar.

Thanks in advance for any insight you can provide.
 
Old 02-04-2021, 04:22 PM   #2
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,269
Blog Entries: 24

Rep: Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206
I would first ask how you arrived at the expression you are using? Why does it include "&#7" 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?
 
Old 02-04-2021, 05:41 PM   #3
Mechanikx
Member
 
Registered: Jul 2018
Distribution: Slackware
Posts: 351

Rep: Reputation: 258Reputation: 258Reputation: 258
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
 
Old 02-04-2021, 05:46 PM   #4
__John_L.
LQ Newbie
 
Registered: Feb 2021
Posts: 14

Original Poster
Rep: Reputation: Disabled
The forum software "edited" my desired target string.
Quote:
ampersand-hash-7-E-semicolon
The curly braces are standard for a sed command-line script, by my understanding.

The ampersand is the issue, I believe. It is a sed special character.

Last edited by __John_L.; 02-04-2021 at 05:52 PM.
 
Old 02-04-2021, 05:52 PM   #5
Ser Olmy
Senior Member
 
Registered: Jan 2012
Distribution: Slackware
Posts: 3,345

Rep: Reputation: Disabled
The curly braces are the problem (and I've never seen them used with sed before). This works:
Code:
echo blah~blah | sed 's/~/\&#7E;/'
And so does this:
Code:
echo blah~blah | sed "s/~/\&#7E;/"
And this:
Code:
echo blah~blah | sed {s/~/\\\&#7E\;/}
Note the double escaping in the last example, which is necessary due to both \ and & being treated as special characters in that context.

Edit: The forum messed up the characters again.

Last edited by Ser Olmy; 02-04-2021 at 06:09 PM.
 
1 members found this post helpful.
Old 02-04-2021, 05:59 PM   #6
__John_L.
LQ Newbie
 
Registered: Feb 2021
Posts: 14

Original Poster
Rep: Reputation: Disabled
Thanks all. My ignorance is showing.
 
Old 02-04-2021, 07:00 PM   #7
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,142

Rep: Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123
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.
 
Old 02-04-2021, 08:10 PM   #8
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,269
Blog Entries: 24

Rep: Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206
Quote:
Originally Posted by __John_L. View Post
I'd like to replace "~" with "&#7E;" 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:

Code:
echo 'blah~blah' |sed 's/~/\&#7E;/g'
blah&#7E;blah

Last edited by astrogeek; 02-04-2021 at 08:19 PM. Reason: forum de-mangle
 
Old 02-05-2021, 07:44 AM   #9
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,616

Rep: Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555
Quote:
Originally Posted by astrogeek View Post
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.)


Last edited by boughtonp; 02-05-2021 at 07:45 AM.
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] sed escape characters issue bmxakias Programming 11 01-01-2020 11:33 AM
shell: how to escape (or not escape) $ within an echo statement? Paul_N Linux - Newbie 2 04-05-2016 01:59 AM
Escape Characters in linux shunraj Linux - Software 1 05-18-2004 03:21 PM
Handle escape characters in a string Helene Programming 7 05-01-2004 11:43 PM
escape characters not escaping BobNz Linux - Software 2 04-09-2004 03:34 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:57 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration