Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

problem in running UNIX command through perl

by kprasanna_79 (Hermit)
on Oct 27, 2008 at 21:41 UTC ( [id://719839]=perlquestion: print w/replies, xml ) Need Help??

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

Revered Monks,
I am trying to run the egrep command through perl. When i tried the same command in UNIX it works fine but creates a zero byte file when i ran through Perl
the unix command i ran is egrep -f temp_qc_file.txt temp_qc_running_file.txt > temp_file.txt My perl code is `egrep -f "$_proj_file" "$_running_file" > "$_admin_temp_file"`; I even printed the filenames ($_proj_file, $_running_file,$_admin_temp +_file) and its correct.
I get a zero byte file of $_admin_temp_file when i run through perl. But it gives the matched output when i run through UNIX.
Any idea why this is happening
-Prasanna.K

Replies are listed 'Best First'.
Re: problem in running UNIX command through perl
by toolic (Bishop) on Oct 27, 2008 at 23:42 UTC
    Check the status of your command using system, and try getting rid of all those quotes:
    my $cmd = "egrep -f $_proj_file $_running_file > $_admin_temp_file"; print $cmd, "\n"; my $result = system $cmd; print "result of egrep = $result\n"

      The quotes shouldn't interfere and would be required if he had spaces in his filenames for example

      Confucius says kill mosquito unless cannon
        It sometimes makes the code more readable to delimit strings in qq{}:
        my $cmd = qq{ egrep -f "$_proj_file" "$_running_file" > "$_admin_temp_ +file" }; my $result = `$cmd`; # or even qx{$cmd}
        That keeps you from having to escape quotes, etc.
Re: problem in running UNIX command through perl
by ikegami (Patriarch) on Oct 27, 2008 at 22:03 UTC

    I sense with a pattern with your questions... Don't ask Perl what the error is, surely a monk can sense it.

Re: problem in running UNIX command through perl
by ig (Vicar) on Oct 27, 2008 at 22:37 UTC

    Does egrep produce an error message? If so, what is it?

    What is the exit status of the failing egrep command?

Re: problem in running UNIX command through perl
by ccn (Vicar) on Oct 28, 2008 at 06:16 UTC

    What happens if you do the following within perl?

    `egrep -f temp_qc_file.txt temp_qc_running_file.txt > temp_file.txt`;
    i.e. just a string wihout variables.

    If the problem is still there, than check paths. Do you run you perl script from the same location as unix command?

Re: problem in running UNIX command through perl
by rovf (Priest) on Oct 28, 2008 at 09:52 UTC
    `egrep -f "$_proj_file" "$_running_file" > "$_admin_temp_file"`;

    Of course the question is why you use `...` instead of system(...), if you are not interested in the output of the egrep, but maybe egrep is writing something to stderr, and the way you code it, that stderr gets lost. I suggest you try

    my $errmsg=`egrep -f "$_proj_file" "$_running_file" > "$_admin_temp_fi +le"`; print "egrep error: $errmsg\n" if $errmsg;
    and see what happens.

    -- 
    Ronald Fischer <ynnor@mm.st>
Re: problem in running UNIX command through perl
by zentara (Archbishop) on Oct 28, 2008 at 13:08 UTC
    I made 2 files: patternfile and testfile, each containing 1 word "perl". The following worked. So I'm guessing your variable filenames are screwed up, or you don't know that the -f option precedes a patternfile, OR you are not getting any matches.
    #!/usr/bin/perl my $ouput = `egrep -f patternfile testfile > output`; print "$output\n";
    #prints nothing, but output contains the word perl

    The following also works

    #!/usr/bin/perl my $p = 'patternfile'; my $t = 'testfile'; my $o = 'output'; my $ouput = `egrep -f $p $t > $o`; print "$output\n";

    I'm not really a human, but I play one on earth Remember How Lucky You Are

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://719839]
Approved by planetscape
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2024-04-25 18:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found