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

Hi,

I am looking for a one-liner that takes an utf8 encoded text-file and prints every line with a bullet-point character added in front (this is for displaying via conky).

My fist "cowboy"-attempt was:

perl -ne 'print "\N{U+2022} $_"' todo.txt
But that is of course not correct and produces the "wide character in print"-warning.

So I did this:

perl -MEncode -ne 'print encode('utf8',"\N{U+2022}"), " $_"' todo.txt
And this seems to do the trick, but it's quite a mouthful for the simple task at hand.

So my question is:

Is this the way to do it or is there a shorter/better way?

Many thanks!

Replies are listed 'Best First'.
Re: Unicode in one-liner
by 1nickt (Canon) on Dec 26, 2016 at 22:55 UTC

    If you are on a system with a UTF8 locale (e.g. my default installation of Ubuntu), when you pass the -C flag to the interpreter, "the standard I/O handles and the default open() layer are UTF-8-fied" (you can specify encoding via an argument to -C if you need to; check with locale -a).

    perl -nC -E 'print "\N{U+2022} $_"' /usr/share/dict/words
    ... • zooms • zoos • zucchini • zucchini's • zucchinis • zwieback • zwieback's • zygote • zygote's • zygotes • Ångström • éclair • éclair's • éclairs • éclat • éclat's • élan • élan's • émigré • émigré's • émigrés • épée • épée's • épées • étude • étude's • études

    Hope this helps!


    The way forward always starts with a minimal test.
      Great, I knew there was a nicer way.

      Unfortunately there does not seem to be a way to combine all the flags (the way you can combine "n" and "e" or "E"), so one has to live with two flag-groups.

      As the "ne"-combination is something I am already used to, I am going to use

      perl -C -ne '...'
      Many thanks!

        Unfortunately switches

        :D

        I never use switches ;)

        perllanse " print join q{,}, map { qq{\x22$_\x22} } @F " perllansE " say join q{,}, map { qq{\x22$_\x22} } @F " perllCSDanse " print join q{,}, map { qq{\x22$_\x22} } @F " perllCSDansE " say join q{,}, map { qq{\x22$_\x22} } @F "

      I like using names where possible.

      perl -nC -E 'print "\N{BULLET} $_"' /usr/share/dict/words

        Hi shawnhcorey,

        perl -nC -E 'print "\N{BULLET} $_"' /usr/share/dict/words

        Note that the automatic loading of charnames was added in v5.16. For older versions of Perl, there's either 1nickt's one-liner, or:

        perl -Mcharnames=:full -nC -E 'print "\N{BULLET} $_"' /usr/share/dict/ +words

        Regards,
        -- Hauke D

Re: Unicode in one-liner
by BrowserUk (Patriarch) on Dec 26, 2016 at 20:07 UTC

    See Perlrun -C?. -CO will probably do the trick.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". The enemy of (IT) success is complexity.
    In the absence of evidence, opinion is indistinguishable from prejudice.