在perl单线程中添加

时间:2017-06-22 14:10:55

标签: perl integer-arithmetic

我正在尝试使用perl one liner为数字添加1000。这是我试过的:

perl -pi -e "s/ZZZ(\d+)ZZZ/ZZZ\1+1000ZZZ/e" file.txt

我希望将1000添加到ZZZ之间的数字。但我收到错误消息:

Backslash found where operator expected at -e line 1, near "ZZZ\"
Bareword found where operator expected at -e line 1, near "1000ZZZ"
    (Missing operator before ZZZ?)
syntax error at -e line 1, near "ZZZ\"
Execution of -e aborted due to compilation errors.

感谢您的帮助!

1 个答案:

答案 0 :(得分:6)

使用$1并使用连接在RHS上构建一个新字符串:

perl -pi -e 's/ZZZ(\d+)ZZZ/ZZZ . ($1 + 1000) . ZZZ/e' file.txt

注意:这不适用于perl -Mstrict