防止 perl 打印换行符

Joe*_*lon 3 perl command-line

我有这个简单的命令:

printf TEST | perl -nle 'print lc'
Run Code Online (Sandbox Code Playgroud)

哪个打印:

test
?
Run Code Online (Sandbox Code Playgroud)

我想要:

test
Run Code Online (Sandbox Code Playgroud)

...没有换行符。我试过 perl 的,printf但它删除了所有的换行符,我想保留现有的。另外,这不适用于我的第二个甚至不使用的示例print

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

哪个打印:

Bob's Big Boy
?
Run Code Online (Sandbox Code Playgroud)

...还有那个烦人的换行符。我希望有一个像 --no-newline 这样的神奇开关,但我猜它更复杂。

编辑:我已经改变了我echo在示例中的使用printf来澄清问题。一些评论者正确地指出我的问题实际上并没有像写的那样得到解决。

Gil*_*not 5

您只需卸下-l开关,请参阅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 $/.
Run Code Online (Sandbox Code Playgroud)

  • 非常感谢。我从来不知道 `perldoc perlrun` 并且从 `man perl` 那里得到的很少。 (2认同)
  • `-l` 从输入中删除换行符并将它们添加到输出中。`回声测试| perl -ne 'print lc'` 应该在使用和不使用 `-l` 的情况下工作相同,除非 echo 和 perl 不同意换行约定。 (2认同)