1

Hi am writing a script which its need to grep the 6th column of the output using awk command but am getting other output. What is the exact syntax in perl to extract 6th column using awk?

#!/usr/bin/perl
use strict;
use warnings;
use diagnostics;
my $filesystem=`df -h |grep -i dev|grep -vE '^Filesystem|proc|none|udev|tmpfs'`;
print "(\"$filesystem\"|awk '{print \$6}')"

Output :
7831c1c4be8c% ./test.pl
("/dev/disk1     112Gi   43Gi   69Gi    39% 11227595 18084674   38%   /
devfs          183Ki  183Ki    0Bi   100%      634        0  100%   /dev
"|awk '{print  $6}')%                                                                               

Am trying to remove the % how it can be done ?

7831c1c4be8c% cat test.pl
#!/usr/bin/perl
use warnings;
use strict;

open my $FS, q(df -h |) or die $!;
while (<$FS>) {
print +(split)[4], "\n"
    if /dev/i and not /devfs/;
 }
 7831c1c4be8c% ./test.pl
 40%
2
  • what the reason for you is to use awk inside perl ? Commented Mar 28, 2015 at 18:16
  • Give us some input data and what you like from it. I guess it would be possible to solve this using so only grep or awk Commented Mar 28, 2015 at 18:27

2 Answers 2

4

You don't need awk inside Perl.

#!/usr/bin/perl
use warnings;
use strict;

open my $FS, '-|', q(df -h) or die $!;
while (<$FS>) {
    print +(split)[5], "\n"
        if /dev/i and not /^Filesystem|proc|none|udev|tmpfs/;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Nor do you need grep for that matter (and I'm sure there's an alternative to using df :)
@TomFenech: Working on it :)
1

as the previous answer says, you don't need awk or grep system calls in perl. however, I will tell you that one reason your code isn't working is because you never made the awk system call. print does not execute the system call. you would have to use system() to execute it.

anyway fwiw you can also do what you want in a one-liner like so:

df -h | perl -lnae 'next if $F[0] =~ /regex/; print $F[5]'

2 Comments

Why echo "$(df -h)" | ... and not just df -h | ... ?
@user000001 ha you're right. echo is totally unnecessary. as you can tell I use perl alone a lot more than bash

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.