0

I want to replace a set of characters with ' using sed.

This post suggest:

With single quotes around the argument (sed 's/…/…/'), use '\'' to put a single quote in the replacement text.

So, I tried following:

echo 'abcd' | sed 's/[abcd]/\'/g'

But it simply ends up expecting more input:

anir@DESKTOP-4856511:~$ echo 'abcd' | sed 's/[abcd]/\'/g'
>
>
>
> ^C

When I copy pasted echo 'abcd' | sed 's/[abcd]/\'/g' in .sh file and ran, it gave me following error:

anir@DESKTOP-4856511:~/Mahesha999/delete$ ./trysed.sh
./trysed.sh: line 1: unexpected EOF while looking for matching `''
./trysed.sh: line 2: syntax error: unexpected end of file

What the right way to do this? Is it impossible to escape single quote inside single quoted string (and I have to use double quotes only as explained here)?

MsA
  • 2,599
  • 3
  • 22
  • 47
  • Use `sed "s/[abcd]/'/g"` or `sed 's/[abcd]/'"'"'/g'` – Wiktor Stribiżew Sep 04 '20 at 10:57
  • 1
    the post you linked at the end also mentions `'s/ones/one\x27s/'` and `sed 's/ones/two'\''s/'` – Sundeep Sep 04 '20 at 11:00
  • @Sundeep whats that `\x27s` there? – MsA Sep 04 '20 at 11:07
  • @anir : Wouldn't be a `tr abcd "'"` easier than using _sed_? – user1934428 Sep 04 '20 at 11:09
  • @anir `\xNN` allows you to specify a character with two digit hexadecimal value, `27` is for single quote, see https://ascii.cl for mapping.. and [my tutorial](https://learnbyexample.github.io/learn_gnused/breere-regular-expressions.html#escape-sequences) for more details about such escape sequences and how the meaning can differ in replacement section – Sundeep Sep 04 '20 at 11:20
  • @Sundeep got it `\x27` is ascii code for `'` – MsA Sep 04 '20 at 11:20
  • @Sundeep is this issue only limited for replacement text? The first link in my question says so. – MsA Sep 04 '20 at 11:30
  • strictly speaking, this isn't a `sed` issue, but shell issue... bash doesn't allow escaping single quote within single quotes, but it is possible in double quotes.. if you use `-f` option and [provide a file as source of sed commands](https://learnbyexample.github.io/learn_gnused/z-s-and-f-command-line-options.html#file-as-source-of-sed-commands), you won't run into this issue – Sundeep Sep 04 '20 at 11:36

1 Answers1

-1

As the post says:

With single quotes around the argument (sed 's/…/…/'), use '\'' to put a single quote in the replacement text.

So, using your example, you would do:

echo 'abcd' | sed 's/[abcd]/'\''/g'

If you want to replace with just one single quote:

echo 'abcd' | sed 's/[abcd][abcd]*/'\''/g'

The shell does not allow single quotes inside a single quoted string. What the code above does is create three strings (which are not separated by anything):

  • single-quoted string: 's/[abcd]/'
  • unquoted string containing just an escaped single-quote: \'
  • single-quoted string: '/g'

The shell then expands them and because they are not separated, they effectively become joined into a single string.

sed sees: s/[abcd]/'/g

jhnc
  • 11,310
  • 1
  • 9
  • 26