防止perl打印换行符

时间:2014-11-26 17:54:26

标签: perl command-line

我有这个简单的命令:

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以澄清问题。一些评论者指出我的问题实际上并没有像写的那样得到修复。

1 个答案:

答案 0 :(得分:4)

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