cbtshare has asked for the wisdom of the Perl Monks concerning the following question:

Hello All, I have a perl script to search string in a file and then request input from STDIN and then replace that string with the new input.The script works, but I just cant see to get the search and replace part right, I got an error saying file only open for reading, and now when I try to open it for appending , it just wont run
#!/usr/bin/perl use strict; use warnings; my ($found, $pass, $name, $line, @array, @sarray, @files, @names, @pas +sword); my $filename='/home/stain/Downloads/REMOTEopen.xml'; open(XML, "+>> $filename") || die ("Error: $!"); chomp(@array=<XML>); foreach(@array) { @sarray=split(" ", $_); @names = grep /\bName=/, @sarray; @password = grep /\bPassword=/, @sarr +ay; if (@password) { foreach(@names, @password) { print XML qq(Enter the plain text pas +sword for $_, password is currently "$password[0]"\n); chomp($pass=<>); print qq(You will now swap $password[ +0] for Password="$pass"\n); print "continue? y/n\n"; chomp(my $ans=<>); if ($ans =~/y/im) { s/$password[0]/Passwor +d=$pass/ ; print XML $_; } } } }

Replies are listed 'Best First'.
Re: Script won't run
by Athanasius (Archbishop) on Nov 22, 2015 at 07:27 UTC

    Hello cbtshare, and welcome to the Monastery!

    Further to 1nickt’s post, if you open a file with +>> as the mode, the filehandle’s position is set to the end of the file. To read from the beginning, you need to add a seek:

    seek XML, 0, 0;

    However, as 1nickt says, this won’t solve the problem. You want to update passwords within the file, and appending new data at the end of the file can’t accomplish that.

    One approach is to use the core module Tie::File:

    use strict; use warnings; use Tie::File; my $filename = ...; tie my @array, 'Tie::File', $filename or die "Cannot tie file '$filename' to array: $!"; for my $line (@array) { my $newline = $line; # Make a copy while ($line =~ / Name=(.*?) \s+ Password=(.*?) (?:\s+|$) /gx) { my $name = $1; my $password = $2; print "Enter the plain text password for $name, ", "password is currently '$password'\n"; chomp(my $newpass = <>); print "You will now swap $password for Password='$newpass'\n"; print "Continue? (y/n)\n"; chomp(my $answer = <>); # Change the copy $newline =~ s{Password=\Q$password\E}{Password=$newpass} if $answer =~ /y/i; } $line = $newline; # Update this line in the file } untie @array or die "Cannot untie file '$filename': $!";

    Note that it’s necessary to make the progressive password substitutions to a copy of $line, because changing $line within the while loop resets the /g match position, resulting in an infinite loop. See, e.g., “Global matching” in Using regular expressions in Perl.

    Hope that helps,

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

      Ok, thank you all for your comments and new approach, I will give the new approach a try now and let you know. also, I should have mentioned this is the structure of the file I am reading:
      <Node Name="MUN" Type="Connection" Descr="" Icon="mRemoteNG" Panel="Ge +neral" Username="root" Domain="" Password="a/5pqLSsMnvRfylxdN6coiFTdc +2+$ <Node Name="ubunugit" Type="Connection" Descr="" Icon="mRemoteNG" Pane +l="General" Username="root" Domain="" Password="bBxKPKXIhw1wKEV9aEUN1 +yynoRGOWT$ </Node>
      Thank you

        I meant to say earlier, if your input is XML — as indicated by the file extension and filehandle name in your OP — then you’ll be better off reading it with a dedicated module, such as XML::LibXML or XML::Twig.

        However, the file excerpt you show here is not valid XML.

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

      Thank you Athanasius for your input and sample script, it works, but I have an issue with the formatting. the line below
      print "Enter the plain text password for $name, ", "password is currently '$password'\n";
      print out the entire line but I want only the name and password to show up it shows
      Enter the plain text password for "Q9" Type="Container" Expanded="True +" Descr="" Icon="mRemote" Panel="General" Username="" Domain="", pass +word is currently '""'
      I tried the following to just get the name and password only but it only returns 0 and 1 lol
      my @names = grep /\bName=(.*?)/ , @array; my @password = grep /\bPassword=(.*?)/ , @array; my $newline = $line; # Make a copy print "name is $names[0] and password is $password[0]\n";
      Also can you please tell me what the following means from your code.
      ?:\s+|$) /mgx also Q$password\E}
        ... can you please tell me what the following means from your code.

        ?:\s+|$) /mgx
        also

        Q$password\E}

        This should be  (?:\s+|$) (a non-capturing grouping),
        and  s{Password=\Q$password\E}{...} (the match regex of a substitution).

        To explain these:

        c:\@Work\Perl\monks>perl -wMstrict -le "use YAPE::Regex::Explain; ;; print YAPE::Regex::Explain->new('(?:\s+|$)')->explain; ;; my $password = 'f+o*o?[ ]b{}r'; my $regex = qr/Password=\Q$password\E/; print YAPE::Regex::Explain->new($regex)->explain; " The regular expression: (?-imsx:(?:\s+|$)) matches as follows: NODE EXPLANATION ---------------------------------------------------------------------- (?-imsx: group, but do not capture (case-sensitive) (with ^ and $ matching normally) (with . not matching \n) (matching whitespace and # normally): ---------------------------------------------------------------------- (?: group, but do not capture: ---------------------------------------------------------------------- \s+ whitespace (\n, \r, \t, \f, and " ") (1 or more times (matching the most amount possible)) ---------------------------------------------------------------------- | OR ---------------------------------------------------------------------- $ before an optional \n, and the end of the string ---------------------------------------------------------------------- ) end of grouping ---------------------------------------------------------------------- ) end of grouping ---------------------------------------------------------------------- The regular expression: (?-imsx:Password=f\+o\*o\?\[\ \]b\{\}r) matches as follows: NODE EXPLANATION ---------------------------------------------------------------------- (?-imsx: group, but do not capture (case-sensitive) (with ^ and $ matching normally) (with . not matching \n) (matching whitespace and # normally): ---------------------------------------------------------------------- Password=f 'Password=f' ---------------------------------------------------------------------- \+ '+' ---------------------------------------------------------------------- o 'o' ---------------------------------------------------------------------- \* '*' ---------------------------------------------------------------------- o 'o' ---------------------------------------------------------------------- \? '?' ---------------------------------------------------------------------- \[ '[' ---------------------------------------------------------------------- \ ' ' ---------------------------------------------------------------------- \] ']' ---------------------------------------------------------------------- b 'b' ---------------------------------------------------------------------- \{ '{' ---------------------------------------------------------------------- \} '}' ---------------------------------------------------------------------- r 'r' ---------------------------------------------------------------------- ) end of grouping ----------------------------------------------------------------------
        Remember that  \Q...\E metaquotes any non-\w character literal or interpolated character. See the  \Q \L \l \U \u \E interpolation control escape sequences in Quote and Quote-like Operators in perlop; see also quotemeta.


        Give a man a fish:  <%-{-{-{-<

Re: Script wont run
by 1nickt (Canon) on Nov 22, 2015 at 04:42 UTC

    Reading from and writing to the same open filehandle is tricky.

    It may be better to open the file in read mode, read in the data, close the file, process the data, open again in write mode ( '>' ) and write the whole file anew.

    You could also adapt the technique to save a backup copy of the file before you write out the new data.

    HTH

    The way forward always starts with a minimal test.
Re: Script wont run
by BillKSmith (Monsignor) on Nov 22, 2015 at 15:09 UTC
    Consider using the runtime flag -i (Refer perlrun).
    Bill
Re: Script wont run
by u65 (Chaplain) on Nov 22, 2015 at 11:21 UTC

    Welcome, cbtshare! For future reference, consistent formatting in your code would be more inviting of comment from passing monks. Your code is, shall we say, not pleasant to study. Suggest more use of the preview function until you get satisfactory results.