我有这个简单的命令:

printf TEST | perl -nle 'print lc'

哪个打印:
test
​

我想:
test

...没有换行符。我尝试了 perl 的 printf ,但它删除了所有换行符,我想保留现有的换行符。另外,这不适用于我的第二个甚至不使用 print 的示例:
printf "BOB'S BIG BOY" | perl -ple 's/([^\s.,-]+)/\u\L$1/g'

哪个打印:
Bob's Big Boy
​

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

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

最佳答案

您只需删除 -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 $/.

关于perl - 防止 perl 打印换行符,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27155848/

10-11 23:15
查看更多