in reply to Re: using command line switches to edit file
in thread using command line switches to edit file

The test-file contents are of the form :

<PROP GUID="471138786526914052" EXPR="≪E The_Rounding_Unit=&Quot;1e-16&Quot; Absolute_Error_Tolerance=&Quot;1e-15&Quot; /&Gt";"/>

How do i extract the numerical values (in bold) from the above fileformat? I need to substitute it with the user input from the command line.
while (<INFO>) { @line=<INFO>; #will this help ? my @new2; if($var{param}) { list_param($var{param}); } if($var{the}) { list_the($var{the}); } sub list_param($) { if ($line =~ m:Absolute_Error_Tolerance=&quot:) { $line=$line1; @new2=split(/\&quot;/,$line1); $line1=~s/$new2[1]/$G{param}/; } }

Replies are listed 'Best First'.
Re^3: using command line switches to edit file
by holli (Abbot) on Feb 13, 2006 at 14:13 UTC
    @line=<INFO>; #will this help ?
    Not at all. That construct will read the whole file into an array lines. That's not what you want here. Have a look at this code.
    my $file = $ARGV[0]; my $repl = $ARGV[1]; open INFO, "<", $file or die $!; while (<INFO>) { s/(Absolute_Error_Tolerance=&Quot;)1e-15(&Quot)/$1$repl$2/; print; } close INFO;
    That can be easily made into a one-liner:
    perl -n -e "s/(Absolute_Error_Tolerance=&Quot;)1e-15(&Quot)/$1$ARGV[0] +$2/; print;" filename
    just this doesn't work. You cannot pass additional variables into an -e "code" statement. However, you avoid problem by putting that line into a shell script or batchfile (here windows batchfile, as i have no idea of shell scripts).
    @echo off perl -n -e "s/(Absolute_Error_Tolerance=&Quot;)1e-15(&Quot)/$1%2$2/; p +rint;" %1
    So you can then call that file using
    batchfilename datafile replacement
    Note: The %1 and %2 are no perl hashes but variables from the shell. They get substituted by the command line values before! the script is run.


    holli, /regexed monk/
      Thankyou :) ..