Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

replacing close-single-quote with apostrophe

by BernieC (Pilgrim)
on Nov 07, 2021 at 21:20 UTC ( #11138555=perlquestion: print w/replies, xml ) Need Help??

BernieC has asked for the wisdom of the Perl Monks concerning the following question:

This should be easy but I can't quite figure a way to do it. I have a text file which is nothing special, except that it uses close-single-quotes {as it should, actually :o)} as apostrophes. I've examined the file and what it has is the sequence {hex} e2 80 99 everyplace a ' should go. It should be easy but I can't sort out how to write a s/???/'/g that'd work.
  • Comment on replacing close-single-quote with apostrophe

Replies are listed 'Best First'.
Re: replacing close-single-quote with apostrophe
by choroba (Cardinal) on Nov 07, 2021 at 21:28 UTC
    You can use the \x notation in a regex:

    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>) {

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

      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;"


        perl -CSD -Mutf8 -pe"s/í/'/g;"
      Perfect -- exactly what I was looking for.. THANKS
Re: replacing close-single-quote with apostrophe
by LanX (Saint) on Nov 08, 2021 at 01:17 UTC
    That's the utf8 encoding of the codepoint of Right Single Quotation Mark

    You should better rely on proper encoding of your script and the input layer and just write s/`/'/g like choroba showed you in the second part.

    Fiddling with bytes instead of characters isn't a clean approach.

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

Re: replacing close-single-quote with apostrophe
by BillKSmith (Monsignor) on Nov 08, 2021 at 15:34 UTC
    As others have already shown, it is best to separate the functions of decode and translation. The following code demonstrates a few minor improvements. The use of character names allows you to type your script in pure ascii. It makes your intention clear without concern about how very similar looking graphics are displayed (Probably the same reasons that you used informal character names in your post). I prefer the use of the tr/// operator rather than s///g for fixed substitutions such as this.
    use strict; use warnings; use Encode 'decode'; use Test::More tests=>1; my $in_file = qq(This isn\xe2\x80\x99t hard); my $text = decode('utf-8', $in_file); $text =~ tr/\N{RIGHT SINGLE QUOTATION MARK}/\N{APOSTROPHE}/; is( $text, q(This isn't hard), 'quote mark test' );


    1..1 ok 1 - quote mark test

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11138555]
Approved by choroba
Front-paged by Corion
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (3)
As of 2023-09-25 13:20 GMT
Find Nodes?
    Voting Booth?

    No recent polls found