Search code examples
perlcommand-line

Prevent perl from printing a newline


I have this simple command:

printf TEST | perl -nle 'print lc'

Which prints:

test
​

I want:

test

...without the newline. I tried perl's printf but that removes all newlines, and I'd like to keep existing one's in place. Plus, that wouldn't work for my second example that doesn't even use print in it:

printf "BOB'S BIG BOY" | perl -ple 's/([^\s.,-]+)/\u\L$1/g'

Which prints:

Bob's Big Boy
​

...with that annoying newline as well. I'm hoping for a magical switch like --no-newline but I'm guessing it's something more involved.

EDIT: I've changed my use of echo in the examples to printf to clarify the problem. A few commenters were correct in stating that my problem wouldn't actually be fixed as it was written.


Solution

  • You simply have to remove the -l switch, see perldoc perlrun

    -l[octnum]
        enables automatic line-ending processing. It has two separate
        effects. First, it automatically chomps $/ (the input record
        separator) when used with -n or -p. Second, it assigns $\ (the output
        record separator) to have the value of octnum so that any print
        statements will have that separator added back on. If octnum is
        omitted, sets $\ to the current value of $/.