Beefy Boxes and Bandwidth Generously Provided by pair Networks Frank
We don't bite newbies here... much
 
PerlMonks  

Re: How to Execute unix commands like "WC, tail, Gunzip, ZIP" against a file which is read from directory

by soonix (Chaplain)
on Jun 06, 2013 at 05:20 UTC ( #1037370=note: print w/ replies, xml ) Need Help??


in reply to How to Execute unix commands like "WC, tail, Gunzip, ZIP" against a file which is read from directory

Alternatively, instead of

system("wc -l $file");
you could do
system("cd $dir; wc -l $file");
which is less efficient, but if instead of wc you have another command, which works in the current directory, this might even be better.

Of course, most efficient is chdir before you loop over the files (as suggested by smls)


Comment on Re: How to Execute unix commands like "WC, tail, Gunzip, ZIP" against a file which is read from directory
Select or Download Code
Re^2: How to Execute unix commands like "WC, tail, Gunzip, ZIP" against a file which is read from directory
by rajsai28 (Novice) on Jun 07, 2013 at 03:10 UTC
    Hi Monks,

    Thanks for your valuable suggestion.

    i have used the below code and it works for me

    can we store the value of unix command in some variable because when we execute the unix command it will either return 0 or 1, i want to store the value

    chdir($dir); foreach my $file (@files) { if ($file=~ m/orig/) { my $lcmd = system("gunzip -c $file|tail -1"); }

    Output:

    123456|345627899292|000108418|

    i want to store the value 123456|345627899292|000108418| in variable so that i can use substr to fetch '000108418'

    Thanks a lot for the valuable inputs

    thanks & regards,
      Hi Monks,

      i got the syntax and please check below

      my $result = `gunzip -c $file|tail -1`;

      where i can get the unix command result

        Maybe you want to read about what the "backticks" operator does. Consult perlop about qx or "backticks".

        Also, in your code snippet, the variable $result was given a name that might indicate its use.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (8)
As of 2014-04-21 11:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (493 votes), past polls