Beefy Boxes and Bandwidth Generously Provided by pair Networks Cowboy Neal with Hat
There's more than one way to do things
 
PerlMonks  

Re: using command line switches to edit file

by physi (Friar)
on Feb 13, 2006 at 07:17 UTC ( [id://529804]=note: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.


in reply to using command line switches to edit file

Hmm, don't really understand what you are doing there, but:
while (<INFO>) { $line=<INFO>; } close INFO; my @new2; if($var{param}) { list_param($var{param}); } if($var{the}) { list_the($var{the}); }
You read in the whole file, and do nothing, but overwrite your $line. After theat you call yout sub's with the $line, which now contains only the last line of your inputfile. Maybe you should try to put the sub-calls into the while loop!? And if you want to replace everytime the same, than this maybe will work to:
perl -p -e 's/yourstring/yournewstring/g' inputfile > outputfile
But that might not that flexible as you need...
-----------------------------------
--the good, the bad and the physi--
-----------------------------------

Replies are listed 'Best First'.
Re^2: using command line switches to edit file
by kitty (Novice) on Feb 13, 2006 at 08:35 UTC
    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}/; } }
      @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 :) ..

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://529804]
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.