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

New User

Oct 29, 2013, 6:04 PM

Views: 1498
using AWK in perl scripts

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}'

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.

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

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

Veteran / Moderator

Oct 30, 2013, 6:47 AM

Views: 1483
Re: [thewolf555] using AWK in perl scripts

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.


use strict;
use warnings;

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