Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Find pieces of text in a file enclosed by `@` and replace the inside

by kcott (Archbishop)
on Mar 08, 2013 at 13:32 UTC ( #1022417=note: print w/replies, xml ) Need Help??


in reply to Find pieces of text in a file enclosed by `@` and replace the inside

G'day kluther,

Rather than using all those substitutions, you can do a single transliteration. Here's a commandline example:

$ perl -Mstrict -Mwarnings -Mutf8 -e '
    binmode STDOUT => ":utf8";
    while (<>) {
        s/@([^@]+)@/$_ = $1; y{abcABC}{абцАБЦ}; $_/eg;
        print;
    }
'
@abc@ abc @ABC@
абц abc АБЦ
cba @cba CBA@
cba цба ЦБА

-- Ken

  • Comment on Re: Find pieces of text in a file enclosed by `@` and replace the inside

Replies are listed 'Best First'.
Re^2: Find pieces of text in a file enclosed by `@` and replace the inside
by kluther (Novice) on Mar 08, 2013 at 14:43 UTC
    Hi Ken, I on a sco-6 machine and that machine doens't have coding for cyrillic.

      That would have been useful information to have provided upfront. Perhaps you'd care to update your initial post with this limitation. You can still do the transliteration - this code produces the same output as my previous solution:

      $ perl -Mstrict -Mwarnings -e ' binmode STDOUT => ":utf8"; my $subs = join q{} => map chr, 0x430, 0x431, 0x446, 0x410, 0x411, + 0x426; while (<>) { s/@([^@]+)@/"\$_ = \$1; y{abcABC}{$subs}; \$_"/eeg; print; } '

      -- Ken

        Hi Ken, tested your code, but got an error on the second word surrounded by '@'. Error: panic: sv_pos_b2u: bad byte offset at .....etc.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1022417]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2022-08-15 04:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?