in reply to sequential substitutions

As stated it is simple enough, but there is usually more to the story, requiring more complex parsing than just detecting digits and substituting.
( echo '<foo>3</foo>'; echo '<foo>14</foo>'; echo '<foo>159</foo>') | +perl -e ' my $x = 0; while (my $l = <>) { $x++; $l =~ s/\d+/$x/; print $l; }' <foo>1</foo> <foo>2</foo> <foo>3</foo>

One world, one people

Replies are listed 'Best First'.
Re^2: sequential substitutions
by Anonymous Monk on Aug 03, 2018 at 10:48 UTC

    There is a little bit more to it (but not much), and I do apologize for over-simplifying.

    Let's suppose the code is actually:

    <foo>3</foo> <bar>a</bar> <foo>14</foo> <bar>bc</bar> <foo>159</foo> <bar>def</bar> [...]

    I want to simply echo lines that do not contain the "foo" tag to the output file.

      Like this?

      use strict; use warnings; my $i = 0; while(<DATA>){ s/<foo>.*?<\/foo>/"<foo>".++$i."<\/foo>"/e; print; } __DATA__ <foo>3</foo> <bar>a</bar> <foo>14</foo> <bar>bc</bar> <foo>159</foo> <bar>def</bar>

        Basically, yes, though I am going to be doing it all at once (using a technique described in a later answer) - reading in the entire file, and then processing it with:

        $fragment =~ s/<foo>\d+<\/foo>/'<foo>' . $i++ . '<\/foo>'/eg;

        Thank you.