in reply to Re: searching for a substring in a string
in thread searching for a substring in a string

Thank you

If I want to update the line in the same file, how do I do it without just appending to the last line.

Is there a way I can just update a single line wherever it exists in the file to the same location?

Thanks

  • Comment on Re^2: searching for a substring in a string

Replies are listed 'Best First'.
Re^3: searching for a substring in a string
by punch_card_don (Curate) on Feb 02, 2005 at 18:47 UTC
    Clunky, but clear, change that line in the array and re-write the file:
    open ( FH, "p.txt" ); my @output = <FH>; close ( FH ); open ( FH, ">p.txt" ); for $i (0 ..#@output) { if ($output[$i] =~ /.*(test.*\.txt).*/) { $output[$i] =~ s/.*(test.*\.txt).*/test29-01-05\.txt/ last; } for $i (0 ..#@output) { print "$output[$i]"; } close ( FH );

    Forget that fear of gravity,
    Get a little savagery in your life.

      Thank you all for the reply.

      Is there a way to check also after how many semi-colons the particular string is located in a string?

      So, if

      myFiles = test1234.txt; hello3456.txt; world7890.txt;

      Then, test1234.txt is located after Zero semicolons

      and hello3456.txt is located after One semicolons

      and world7890.txt is located after Two semicolons

Re^3: searching for a substring in a string
by kutsu (Priest) on Feb 02, 2005 at 17:54 UTC

    I don't know if I'm reading you right but: perl -p -i.bkup -e "s/.*(test.*?\.txt).*/test29-01-05\.txt/;" p.txt, might do what you want. (see perlrun)

    "Cogito cogito ergo cogito sum - I think that I think, therefore I think that I am." Ambrose Bierce