简体   繁体   中英

Addition in perl one-liner

I'm trying to add 1000 to numbers with a perl one liner. Here is what I tried:

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

I was hoping that would add 1000 to numbers between ZZZ . But I get the error message:

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.

Thanks for any help!

Use $1 and build a new string on the RHS using concatenation:

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

Note: This does not work with perl -Mstrict

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM