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

Hi. I don't know anything about Perl but I got some help elsewhere for how to use Perl to change a selection of Spanish or any other language with the Roman alphabet into hyphens and spaces. This would be useful for my students learning Spanish.

The person who helped me came up with the command perl -pi -e 's/\S/\-/g'

With a crash course in using BB Edit and Terminal for the mac I've managed to use it. The only problem is the code turns commas, full stops, colons, and semi-colons into hyphens. I want to use it so students can write down the Spanish they are listening to over the hyphens and if the hyphens include punctuation marks this will confuse people. It didn't occur to me this problem beforehand unfortunately.

Is it possible to come up with an amended command that would covert only words into hyphens and spaces and leave the punctuation intact? I don’t know if this is an impossible task to ask for or is simple to do. I would be delighted if someone could come up with it. Thanks.

  • Comment on Converting a passage of Spanish into hyphens and spaces

Replies are listed 'Best First'.
Re: Converting a passage of Spanish into hyphens and spaces
by Corion (Patriarch) on Sep 13, 2015 at 10:52 UTC

    See perlre and perlrecharclass for what the parts in a regular expression mean.

    \S means "any non-whitespace", but you don't want that, you want "any alphabetical character". I don't know what encoding your text is in, but if it is Latin-1, the following will work:

    perl -pi -e 's/[a-z]/-/gi'

    If your text file is encoded as (for example) UTF-8 or Unicode, I'm not sure if a oneliner will work. The following is untested but could work:

    perl -CIO -pi -e 's/[[:alpha:]]/-/gi'
      Thank you Corion. My piece of text was saved in UTF-8. I couldn't get either command to work. I tried saving a text file in Western ISO Latin 1. Is that the same as Latin-1? Anyway I couldn't get that to work either. Dumb question: if I pasted some error messages does that tell you everything you need to know?

        I'm not sure if that will tell me everything I need to know, but the error messages will certainly help diagnose it better.