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