1

For example I want to change

"'this text'" "that text" 'other_text' 

into

'this text' "that text" 'other_text' 

I tried

sed -e 's/^"\'/"/g' 

but my quoting must be off.

Ubuntu.

1

3 Answers 3

3

With GNU sed:

sed 's|\x22\x27|\x27|;s|\x27\x22|\x27|' file 

Output:

 'this text' "that text" 'other_text' 

See: http://www.asciitable.com/

3
  • 1
    that is so Ugly Commented Sep 17, 2015 at 18:28
  • 1
    You gave me an idea. If you put those ugly numbers into shell variables, then you can use names i.e. ${single_quote} ${double_quote}, this is readable but more characters. Commented Sep 17, 2015 at 18:41
  • @richard: nice idea. Commented Sep 17, 2015 at 18:46
1

You can not use escape \ in a '' quote. Therefore put everything in a "" quote and escape the "s. e.g. "s/^\"'/'/g"

Alternatively end the '' quote, do a \', then start the '' quote again e.g. 's/^"'\''/'\''/g'

Also if you are easily confused by the \s and /s, then note you do not have to use /s as delimiters. You can use any character, e.g. "s%^\"'%'%g"


This only does the first quote at the beginning of line, the bit you seem to be struggling on.

1
  • actually its the second part that I'm struggling on Commented Sep 17, 2015 at 19:36
0

Try this line

sed -e "s/^\"'/\'/g" -e "s/'\"$/\'/g" file 

Instead of enclosing the sed expression between ' ', do it between " " so you can escape with \ the " "

e.g.

@tachomi:~$ echo "\"'this text'\"" "'this text'" @tachomi:~$ echo "\"'this text'\"" | sed -e "s/^\"'/\'/g" -e "s/'\"$/\'/g" 'this text' 

e.g.2

@tachomi:~$ cat file.txt "'this text'" "that text" 'other_text' @tachomi:~$ sed -e "s/^\"'/\'/g" -e "s/'\"$/\'/g" file.txt 'this text' "that text" 'other_text' 
4
  • 1
    You can do multiple -e in one sed e.g. sed -e "s/^\"'/\'/g" -e "s/'\"$/\'/g" file Commented Sep 17, 2015 at 18:48
  • first part of answer works but second part messes up output, unexpected token messages. Commented Sep 17, 2015 at 18:50
  • @MichaelDurrant Try with the update Commented Sep 17, 2015 at 18:52
  • @MichaelDurrant could you please provide the output Commented Sep 17, 2015 at 19:06

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.