in reply to Increment a string with letters and numbers

echo "EVO:0000023" | perl -pe 's/(\d+)/++($_=$1)/e;'

Replies are listed 'Best First'.
Re^2: Increment a string with letters and numbers
by kschwab (Vicar) on Mar 28, 2013 at 21:16 UTC
    upon further examination, already submitted. ah well.

      I have the same question but how is this done if there are for example EVO:001, EV0:002, EVO:003 in a file and I want each one incremented.. I just want to supply EVO: then have it increment the number part. The closest I have gotten is this but it injects a one instead of incrementing the variable

      #!perl=C:\bin\perl use warnings; use strict; use Tie::File; tie my @data, 'Tie::File', 'myfile.txt' or die $!; sub inc { my ($num) = @_; ++$num } s/(MyString\d+)/$1 . (inc($2))/eg for @data; untie @data;

        Using the solution given above:

        13:09 >perl -wE "my $s = 'EVO:001 xyz345 EVO:456'; $s =~ s/(EVO:)(\d+) +/$1.++($_=$2)/eg; say $s;" EVO:002 xyz345 EVO:457 13:13 >

        Hope that helps,

        Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,