Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Re^2: System command not working

by muchyog (Novice)
on Jul 10, 2012 at 06:52 UTC ( #980825=note: print w/replies, xml ) Need Help??

in reply to Re: System command not working
in thread System command not working

Thanks for the reply I am doing it in this way :

#/usr/bin/perl -w use strict ; my $outFile= "output.txt"; my $inFile= "infile.txt"; my $start=17; my $end=30; my $perlcmd="perl -ne 'print if \$. >= $start && \$. <= $end' $inFile +>>$outFile"; system($perlcmd);

This is working what I expect but plz tell is this the best way or their is a fast and better way.

Replies are listed 'Best First'.
Re^3: System command not working
by kcott (Canon) on Jul 10, 2012 at 07:51 UTC

    From your initial shell process, you launch a perl process which launches another shell process which launches another perl process. No, this is not the best way to do things. Except for the system() function, you initially seemed to be on the right track. Here's how I would have tackled this:

    #!/usr/bin/env perl use strict; use warnings; my $infile = q{}; my $outfile = q{pm_read_part_file.out}; my $start = 3; my $end = 5; open my $in_fh, q{<}, $infile or die qq{Can't open $infile for reading +: $!}; open my $out_fh, q{>}, $outfile or die qq{Can't open $outfile for writ +ing: $!}; while (<$in_fh>) { next if $. < $start; last if $. > $end; print $out_fh $_; } close $in_fh; close $out_fh;

    Here's a sample run with file contents:

    en@ganymede: ~/tmp $ ken@ganymede: ~/tmp $ cat Line 1 Line 2 Line 3 Line 4 Line 5 Line 6 ken@ganymede: ~/tmp $ cat pm_read_part_file.out Line 3 Line 4 Line 5 ken@ganymede: ~/tmp $

    -- Ken

Re^3: System command not working
by aitap (Deacon) on Jul 10, 2012 at 07:19 UTC
    You can add -n to the first line of your script and set all needed variables in the BEGIN{} block (which is not looped like all other code when using -n):
    #!/usr/bin/perl -wn BEGIN{ use strict; my $outFile= "output.txt"; my $inFile= "infile.txt"; $main::start=17; # 'my' in this scope will result in uninitialized va +lues outside the BEGIN{} block $main::end=30; close STDOUT; close STDIN; open STDIN,"<",$inFile || die $!; open STDOUT,">",$outFile || die $!; } print if $. >= $start && $. <= $end;
    Sorry if my advice was wrong.
Re^3: System command not working
by monsoon (Pilgrim) on Jul 10, 2012 at 15:04 UTC

    Any time I invoke anything on the OS via 'system' or backticks or any other way, I'd be worried that the program being called may not be in the path of the user running the script.

    If you prefer running your one-liner as an OS command from inside a script, why not run it from a shell script then?

    #!/usr/bin/sh /usr/bin/perl -ne 'print if $. >=17 && $. <= 39' infile.txt >outfile.t +xt
Re^3: System command not working
by Jim (Curate) on Jul 10, 2012 at 22:00 UTC

    It seems someone taught you how to program outside of Perl before she taught you how to program inside of Perl. ;-)

    Here's my riff on kcott's script…

    #!/usr/bin/env perl use strict; use warnings; use autodie qw( open close ); use English qw( -no_match_vars ); my $input_file = 'input.txt'; my $output_file = 'output.txt'; my $start = 17; my $end = 30; open my $input_fh, '<', $input_file; open my $output_fh, '>', $output_file; LINE: while (my $line = <$input_fh>) { next LINE if $INPUT_LINE_NUMBER < $start; last LINE if $INPUT_LINE_NUMBER > $end; print {$output_fh} $line; } close $input_fh; close $output_fh; exit 0;

    And here's a greatly reduced version using <ARGV> and STDOUT instead of hardwiring file names inside the script…

    #!/usr/bin/env perl use warnings; while (<>) { next if $. < 17; last if $. > 30; print; }

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://980825]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2016-09-30 06:24 GMT
Find Nodes?
    Voting Booth?
    Extraterrestrials haven't visited the Earth yet because:

    Results (562 votes). Check out past polls.