1

The following removes the surrounding quotes of the email address string:

$ echo  "john.doe@gmail.com" | sed 's/"([^"]*)"/\0/g'  
john.doe@gmail.com  

But if:

$ cat ~/Desktop/emails.txt 
"john.doe@gmail.com"  
$ sed 's/"([^"]*)"/\0/g' ~/Desktop/emails.txt    
"john.doe@gmail.com"   
$ sed -i '' 's/"([^"]*)"/\0/g' ~/Desktop/emails.txt
$ cat ~/Desktop/emails.txt 
"john.doe@gmail.com"  

Trying to apply the exact same sed regex substitution using a file containing the same string does not work.
What am I doing wrong?

  • Ah it is unquoted because echo takes the whole string without quotes. But why are quotes added then in the file? – Jim Jan 17 '17 at 22:40
9

I am sorry but your echo example does not work. It seems to work because double quote (") are interpreted by bash and never passed to sed.

Note the difference between the following two examples:

$ echo "john.doe@gmail.com" 
john.doe@gmail.com
$ echo "\"john.doe@gmail.com\"" 
"john.doe@gmail.com"

Your echo command does not feed the " to sed so it seems to work because there are no " to remove in input string.

If you try correctly escaping the ", the echo example will do not work as the file example:

$ echo  "\"john.doe@gmail.com\"" | sed 's/"([^"]*)"/\0/g'  
"john.doe@gmail.com"

You sed command has two errors:

  1. You are using the extended regex syntax. You can use it only if you have gnu sed. The difference is in the way they use the parenthesis.
  2. You must count back-references starting by 1.

So the correct command is:

echo  "\"john.doe@gmail.com\"" | sed 's/"\([^"]*\)"/\1/g' 

or, if your sed supports extended regex:

echo "\"john.doe@gmail.com\"" | sed -E 's/"([^"]*)"/\1/g'
3
gv@debian:$ cat a.txt
"john.doe@gmail.com"

gv@debian:$ sed 's#"##g' a.txt #remove all quotes
john.doe@gmail.com

gv@debian:$ cat a.txt |tr -d '"' #remove all quotes
john.doe@gmail.com

gv@debian:$ sed 's#^"##g; s#"$##g' a.txt #remove first and last quote 
john.doe@gmail.com

gv@debian:$ a="\"john.doe@gmail.com\"";echo -e "$a" "\n" "${a: 1:-1}" #remove first and last char
"john.doe@gmail.com" 
 john.doe@gmail.com
1

As mentioned by @andcoz, this

$ sed -i '' 's/"([^"]*)"/\0/g' ~/Desktop/emails.txt

needs the parentheses escaped and the backreference changed from \0 to \1.

After modification, a functional sed command looks like this:

$ sed -i '' 's/"\([^"]*\)"/\1/g' ~/Desktop/emails.txt  

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.