http://qs1969.pair.com?node_id=11138557


in reply to replacing close-single-quote with apostrophe

You can use the \x notation in a regex:
s/\xe2\x80\x99/'/g

If you want to use it in a one-liner, you need to be careful about quoting. In bash, for example, you need to write

perl -pe 's/\xe2\x80\x99/'\''/g' file

or use the \x for the single quote, as well:

perl -pe 's/\xe2\x80\x99/\x27/g' file

Alternatively, if you want to use "smart quote" directly:

use utf8;

open my $in, '<:encoding(UTF-8)', 'file' or die $!;
while (<$in>) {
    s/’/'/g;
    print;
}

map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

Replies are listed 'Best First'.
Re^2: replacing close-single-quote with apostrophe
by hippo (Bishop) on Nov 08, 2021 at 10:34 UTC

    For completeness, you can also use the literal character in a one-liner as well:

    $ echo It’s easy | perl -Cio -pe "s/’/'/g;"
    It's easy
    

    Note that the script, such as it is, is enclosed in double-quotes here only to avoid the problem of escaping the single quote within.


    🦛

      That command is wrong. You want

      perl -CSD -Mutf8 -pe"s/’/'/g;"

      -Cio has no effect in that example, which you can see by removing it.

      $ echo It’s easy | perl -pe"s/’/'/g;" It's easy

      -Ci only has an effect if reading from a file

      $ perl -Cio -pe"s/’/'/g;" <( echo "It’s easy" ) Wide character in print at -e line 1, <> line 1. It’s easy

      This is what you want if reading from a file:

      $ perl -CiO -Mutf8 -pe"s/’/'/g;" <( echo "It’s easy" ) It's easy

      This is what you want if reading from STDIN:

      $ echo "It’s easy" | perl -CIO -Mutf8 -pe"s/’/'/g;" It's easy

      Combining both, you can use

      perl -CiIO -Mutf8 -pe"s/’/'/g;"

      Better:

      perl -CSD -Mutf8 -pe"s/’/'/g;"
Re^2: replacing close-single-quote with apostrophe
by BernieC (Pilgrim) on Nov 07, 2021 at 22:59 UTC
    Perfect -- exactly what I was looking for.. THANKS