How to escape % with sed?


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers How to escape % with sed?
# 1  
Old 05-03-2019
How to escape % with sed?

Hello,
I am running ubuntu 16.04
I searched "how to replace dot by % using sed" but no luck.

file
Code:
My.Name.Is.Earl

Expected output
Code:
My%Name%Is%Earl

I tried:
Code:
sed -i "s|.|[%]|g" file

Code:
sed -i "s|.|{%}|g" file

Code:
sed -i "s|.|\%|g" file

I'd appreciate your help

Thank you
Boris
# 2  
Old 05-03-2019
Hello baris35,

Could you please try following.
Code:
sed 's/\./%/g'  Input_file

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 05-03-2019
To add: in regular expressions, a . (dot) has a special meaning as it stands for "any character" . In order to change the meaning to a literal dot, it can be escaped with a \ character (backslash) preceding the dot. The %-sign is not a special character here and therefore does not need to be escaped.

Last edited by Scrutinizer; 05-03-2019 at 01:27 AM..
This User Gave Thanks to Scrutinizer For This Post:
# 4  
Old 05-03-2019
Code:
tr '.' '%' < file

# 5  
Old 05-03-2019
I focused on % symbol and missed dot to be escaped but I do not remember that I was escaping it earlier.
Thank You All!
# 6  
Old 05-03-2019
Hello Again,
I am accustomed to run sed without escaping the dot. I had not encountered any issue earlier.
Your solution works but could you please explain what is the difference between replacement of . by % and standard replacement as shown in given below example? Why do we escape dot ?

File1: (replacement of dot by percentage)
Code:
My.Name.Is.Earl

Working Solution:
Code:
sed 's/\./%/g'  File1

File2: (removal of .txt.doc extension)
Code:
Test1.test2.txt.doc

Expected:
Code:
Test1.test2

Solution:
Code:
sed -i "s|.txt.doc||g" File2

I have not escaped dot in example two.
Also works when we use rename command; without escaping dot, able to rename the file..

Thank you
Boris

Last edited by baris35; 05-03-2019 at 10:20 AM..
# 7  
Old 05-03-2019
The dot is a special character in regexes, a wildcard. man regex:
Quote:
An atom is a regular expression enclosed in "()" (matching a match for the regular expression), an empty set of "()" (matching the null string)(!), a bracket expression
(see below), '.' (matching any single character), '^' (matching the null string at the beginning of a line), '$' (matching the null string at the end of a line), a '\'
followed by one of the characters "^.[$()|*+?{\" (matching that character taken as an ordinary character), a '\' followed by any other character(!) (matching that
character taken as an ordinary character, as if the '\' had not been present(!)), or a single character with no other significance (matching that character). A '{'
followed by a character other than a digit is an ordinary character, not the beginning of a bound(!). It is illegal to end an RE with '\'.
It also matches dots like in your example 2, but would also work on -txt-doc. To explicitly match a dot in the text, it has to be escaped innregexes to suppress its special meaning.
This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl/sed Escape Syntax Problem . . .

Greetings! I've run into this before; and am having a spot of trouble trying to figure out the way that Perl would prefer the following example syntax to be formed:#!/usr/bin/perl use strict; use warnings; use diagnostics; `sed -i 's/Hi Mom!\|Hi Dad!/Bye Everyone!/I' ./test.txt`;Perl... (5 Replies)
Discussion started by: LinQ
5 Replies

2. Shell Programming and Scripting

Ignore escape sequence in sed

Friends, In the file i am having more then 100 lines like, File1 had the values like this: #Example East.server_01=EAST.SERVER_01 East.server_01=EAST.SERVER_01 West.server_01=WEST.SERVER_01 File2 had the values like this: #Example EAST.SERVER_01=http://yahoo.com... (3 Replies)
Discussion started by: jothi basu
3 Replies

3. Shell Programming and Scripting

sed - with escape character

i have string as below str=".<date>" in which i need to replace < with /< , when i tried with sed , got the output. --> echo $str | sed 's/</\\</g' .\<date> when i tried to assign it to a variable , i am not getting the same --> a=`echo $str | sed 's/</\\</g'` ; echo $a... (4 Replies)
Discussion started by: expert
4 Replies

4. Shell Programming and Scripting

Escape special characters in SED

Need help in escaping special characters in sed command. Here is the the string which i am trying to find a replace with From :- REQUEST_TYPE=PIXEL&amp;MSG_ID={//MESSAGE_ID} To :- REQUEST_TYPE=PIXEL&amp;MSG_ID= X_EDELIVERY_MESSAGE_ID &amp; BATCH_ID= X_EDELIVERY_BATCH_ID Here is the sed command i am... (2 Replies)
Discussion started by: aakishore
2 Replies

5. Shell Programming and Scripting

Need help with sed to escape special characters

Hello Everyone, I need to read an encrypted password from the user and update that value in an xml file. I am trying to use "sed" for searching the appropriate tag and replacing this new value that get from the user. Since the encrypted password can contain special characters(like /,\,&,etc),... (4 Replies)
Discussion started by: majose
4 Replies

6. Shell Programming and Scripting

Escape character in sed

Hello experts I am trying to write a shell script which will add ' ' to a unix variable and then pass it to oracle for inserting to a table. I am running the script as root and I have to do a su -c . The problem is the character ' is not recognised inside sed even after adding escape... (1 Reply)
Discussion started by: pvedaa
1 Replies

7. Shell Programming and Scripting

SED: Replacing $1 with $2 escape problem

Hi all, I have a script which uses sed to replace one string with another. The problem is, the string to be matched, and its replacement are coming in as two command line arguments $1 and $2 $1 and $2 can be absolutely anything, but both should be treated purely as strings. My sed command... (7 Replies)
Discussion started by: mark007
7 Replies

8. Shell Programming and Scripting

Escape character - sed

Hi All, How do i write in sed for the 6th and 7th field of etc/passwd file as it involves "/" character? Does mine below is correct? It's incomplete script as i need help with syntax as i always getting may errors :( Example of etc/passwd file: blah:x:1055:600:blah... (6 Replies)
Discussion started by: c00kie88
6 Replies

9. Shell Programming and Scripting

sed escape char

Hi, For the following complex code , <!-- ... (2 Replies)
Discussion started by: fed.linuxgossip
2 Replies

10. UNIX for Dummies Questions & Answers

possible to escape the \ character in sed?

is it possible to escape the \ character in sed? right now I'm trying to replace all occurances of \ with \\ sed \"s|test|test_replacement|g\" file1 > output; #this works fine sed \"s|\\|\\\|g\" file1 > output; #this generates the following error: sed: -e expression #1, char 17:... (1 Reply)
Discussion started by: gammaman
1 Replies
Login or Register to Ask a Question