CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
using AWK in perl scripts

 



thewolf555
New User

Oct 29, 2013, 6:04 PM

Post #1 of 2 (944 views)
using AWK in perl scripts Can't Post

Hello ,

I am a beginner at perl....or for that matter to scripting. I work as a system admin and wanted to use Perl to automate some daily mundane jobs.

when I run the following command in unix, I get the the desired output

server{160}> ls -l | grep 'Move' | awk '{print $8}'
12:17
12:20

However, when I run the same using a perl script, I get the following output..

server{159}> sudo perl test.pl
-rw-r----- 1 XYZ users 9313 Sep 22 12:17 Move List .txt
-rw-r----- 1 XYZ users 2297 Sep 22 12:20 Move List .txt

here is the simple code I wrote, not sure why the control is not going past grep.

#!/usr/local/bin/perl
my $i;
$i = `ls -l | grep 'Move' | awk '{print $8}'`;
print $i;

Any help in this regard is highly appreciated :)
Thank you


FishMonger
Veteran / Moderator

Oct 30, 2013, 6:47 AM

Post #2 of 2 (929 views)
Re: [thewolf555] using AWK in perl scripts [In reply to] Can't Post

You need to escape the $ so that it gets passed to the shell instead of perl trying to dereference the $8 variable.

$i = `ls -l | grep 'Move' | awk '{print \$8}'`;

However, there is no need to use piped shell commands. Perl has builtin functions to do that and will give you more flexibility in formatting the output.

Here's one example.

Code
#!/usr/local/bin/perl 

use strict;
use warnings;

foreach my $file (glob "Move*") {
print scalar localtime((stat($file))[9]), "\n";
}


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives