I want to batch-process text files from the command line by adding a static offset to all numbers contained in said files using perl.
For example, if the file contained the line: AN_IDENTIFIER_TOKEN = 1,
and the offset is 19
then this line should be transformed to AN_IDENTIFIER_TOKEN = 20,
I tried to use perl -pe 's/(\\d+)$/19+$1/e' file1.txt
but that did produce the same output as input (no numbers were changed). What is the correct command to pass to perl?
Using ActiveState Perl 5.24.1 on Windows.
It should work, try this:
perl -pe "s/(\d+)/19+$1/eg" file1.txt
Your regex (\\d+)$
matched only digits at the end of a string, so that won't match 1,
If you want to match only " = <digits>,"
, then use something like this
perl -pe "s/ = (\d+),/' = '.(19+$1).','/eg" file1.txt
If you want to alter numbers at the ends of the lines in the file, and those numbers are always followed by commas, then you can use tnis
S/(\d+)(?=,\s*$)/$1+19/e
It uses a look-ahead to check that the number is followed by a comma, optional white space, and the end of the line
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.