防止 perl 打印换行符

问题描述 投票:0回答:2

我有这个简单的命令:

printf TEST | perl -nle 'print lc'

哪个打印:

test
​

我想要:

test

...没有换行符。我尝试了 Perl 的

printf
但这会删除 all 换行符,我想保留现有的换行符。另外,这对于我的第二个例子不起作用,因为它甚至没有使用
print

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

哪个打印:

Bob's Big Boy
​

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

编辑:我已将示例中对

echo
的使用更改为
printf
以澄清问题。一些评论者正确地指出我的问题实际上不会像所写的那样得到解决。

perl command-line
2个回答
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 $/.

0
投票

在打印之前添加一个简单的 local 配置:

local $| = 1;  print ...

鸣谢:Perl Monks

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.