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

Update: i Could break down the lengthy question to a simple:
How to overwrite a given line in a file with another line without appending?.
#!/usr/bin/perl use strict; use File::Copy; use warnings; my $open_file= "C:/oracle/ora81/network/ADMIN/SQLNET.ORA"; my $write_file=">>C:/oracle/ora81/network/ADMIN/SQLNET.ORA"; open(IN,$open_file) || die "cannot open $open_file $!"; open(OUT,$write_file) || die "cannot create $write_file $!"; while (<IN>) { if ($_ = /^SQLNET\.AUTHENTICATION_SERVICES=\ \(NTS\)$/){ print "\#\ ^SQLNET\.AUTHENTICATION_SERVICES=\ (NTS)"; print OUT $_ "\#\ SQLNET\.AUTHENTICATION_SERVICES=\ (NTS)"; #Want to write the 2nd string print ... to OUT write handle #OUT $_ } }
I want the print state in the while loop to write to the OUT Filehandle, i got a syntax error if i try this? Any Ideas where my error is? error message:
String found where operator expected at ora.pl line 22, near "$_ "\#\ + SQLNET\.AUTHENTICATION_SERVICES=\ (NTS)"" (Missing operator before "\#\ SQLNET\.AUTHENTICATION_SERVICES=\ (N +TS)"?) syntax error at ora.pl line 22, near "$_ "\#\ SQLNET\.AUTHENTICATION_ +SERVICES=\ (NTS)"" Execution of ora.pl aborted due to compilation errors.


Thanks
MH

Replies are listed 'Best First'.
Re: Search and Replace line by line not working on Filehandle
by cdarke (Prior) on Jan 15, 2010 at 11:06 UTC
    May I suggest that you need to create a new file, commenting out the required line/s, then rename it? For example:
    use warnings; use strict; my $filename = 'C:/oracle/ora81/network/ADMIN/SQLNET.ORA'; open (my $in, '<', $filename) || die "Unable to open $filename:$!"; open (my $out,'>', "$filename.tmp") || die "Unable to open $filename.t +mp:$!"; while (<$in>) { # Identify the record if ($_ eq "SQLNET.AUTHENTICATION_SERVICES= (NTS)\n"){ $_ = '# '.$_; } print $out $_; } close $in; close $out; rename "$filename.tmp",$filename or die "Unable to rename: $!";

      Yes. Your suggestions are always welcome. Thanks that works fine and shows me another (which i think easier to understand way than my solution) which helps me on my way to the intermediate beginner ...


      Thanks
      MH

Re: substitute on file line by line not working syntax error
by Ratazong (Monsignor) on Jan 15, 2010 at 08:27 UTC
    print OUT $_ "\#\ SQLNET\.AUTHENTICATION_SERVICES=\ (NTS)";

    Isn't there a comma missing between $_ and "\#SQL..."?

    Rata
Re: substitute on file line by line not working syntax error
by ikegami (Patriarch) on Jan 15, 2010 at 08:31 UTC
    What's
    $_ "\#\ SQLNET\.AUTHENTICATION_SERVICES=\ (NTS)"
    suppose to do? Are you missing a comma?
      changed this the comma was missing:
      print OUT $_ , "\#\ SQLNET\.AUTHENTICATION_SERVICES=\ (NTS)";
      OK. Now the line prints, i wanted it to overwrite the original line but it prints to the next line maybe i need to "remember" the line number and overwrite it?
      result:
      SQLNET.AUTHENTICATION_SERVICES= (NTS) 1# SQLNET.AUTHENTICATION_SERVICES= (NTS)<p>
      Are the parentheses around (NTS) interpreted as "backreference" and printed before as 1 ...?

      Thanks
      MH
        If you want just the new line, not the old and the new one, don't print the old one ($_).
      original file:
      # SQLNET.ORA Network Configuration File: c:\oracle\ora81\network\admin +\sqlnet.ora # Generated by Oracle configuration tools. NAMES.DEFAULT_DOMAIN = world SQLNET.AUTHENTICATION_SERVICES= (NTS) NAMES.DIRECTORY_PATH= (TNSNAMES) SQLNET.AUTHENTICATION_SERVICES= (NTS)<p>
      The Line SQLNET.AUTHENTICATION_SERVICES= (NTS) needs to be commented/deactived so afterwards it reads:
      # Generated by Oracle configuration tools. ... # SQLNET.AUTHENTICATION_SERVICES= (NTS) ...

      <id://>

      Thanks
      MH
Re: Search and Replace line by line not working on Filehandle
by bv (Friar) on Jan 15, 2010 at 15:23 UTC

    In case you were looking for another solution, you could use Tie::File, then loop over the tied array


    print map{substr'hark, suPerJacent other l',$_,1}(11,7,6,16,5,1,15,18..23,8..10,24,17,0,12,13,3,14,2,4);