Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

How to print a newline in oneliners ...

by karlgoethebier (Priest)
on Apr 22, 2014 at 16:23 UTC ( #1083195=perlquestion: print w/ replies, xml ) Need Help??
karlgoethebier has asked for the wisdom of the Perl Monks concerning the following question:

Hi all, this one should be self explaining:

karls-mac-mini:monks karl$ for i in {0..9}; do echo $i; done | perl -p +e '$_++' 12345678910karls-mac-mini:monks karl$

Ok, but i want a newline:

karls-mac-mini:monks karl$ for i in {0..9}; do echo $i; done | perl -n +e '$_++; print qq($_\n)' 1 2 3 4 5 6 7 8 9 10

Quite simple. Got it. But sometimes i struggle a little bit with the essentials :-(

So i wonder if there is a TMTOWTDI for doing something like this.

Thank you very much for any hint and best regards, Karl

«The Crux of the Biscuit is the Apostrophe»

Comment on How to print a newline in oneliners ...
Select or Download Code
Re: How to print a newline in oneliners ...
by Anonymous Monk on Apr 22, 2014 at 16:35 UTC

    I use the -l switch without a value all the time:

    for i in {0..9}; do echo $i; done | perl -nle '$_++; print'

    It's the equivalent of $\ = $/ and a chomp on the input.

      for in in {0..9}; do echo $i; done | perl -nle 'print ++$_'

        Well if you wanna play that game... ;-)

        for i in {0..9}; do echo $i; done | perl -ple '$_++'
Re: How to print a newline in oneliners ...
by karlgoethebier (Priest) on Apr 22, 2014 at 17:02 UTC

    I really wasn't aware about this switch. From perlrun:

    -l[octnum] enables automatic line-ending processing...

    Thank you and best regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

Re: How to print a newline in oneliners ...
by Laurent_R (Parson) on Apr 22, 2014 at 17:37 UTC
    Why don't you do everything in Perl? Something like this:
    $ perl -le 'print ++$_ for 0..9;' 1 2 3 4 5 6 7 8 9 10
      "Why don't you do everything in Perl?"

      Thank you very much Laurent for answering.

      Sure, you are right.

      But the bash code on the left side of the pipe (for i in {0..9}; do echo $i; done) wasn't the "essential" part of the question.

      The question was just about adding a newline to my Perl output.

      As i mentioned in my OP, i sometimes miss some "essentials".

      In this case the -l switch.

      To be honest, i wondered many times why some folks use -l in oneliners.

      In other words: just yet another D'oh.

      Best regards (et à bientôt), Karl

      «The Crux of the Biscuit is the Apostrophe»

        I think most of us sometimes miss some "essentials". At least, this includes me...

      Yeah, that's what I was wondering. What about:

      perl -E 'say for 1..10'

      Update: I hadn't seen karlgoethebier's answer to Laurent_R when I posted this...

        It's good, but -E doesn't work on the old Perls that are still around. Instead of trying to remember what the Perl version on the current system happens to be, or asking people what version of Perl they're running before giving them a one-liner to use, I've just gotten in the habit of staying backwards compatible and writing perl -le 'print for 1..10'

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1083195]
Front-paged by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2014-12-29 04:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (184 votes), past polls