开发者

regex help on unix df

开发者 https://www.devze.com 2023-03-13 12:03 出处:网络
I need some help tweaking my code to look for another attribute in this unix df output: Ex. FilesystemSizeUsedAvail CapacityMounted on

I need some help tweaking my code to look for another attribute in this unix df output:

Ex.

Filesystem     Size    Used   Avail Capacity  Mounted on
/dev/ad4s1e     61G     46G    9.7G    83%    /home

So far I can extract capacity, but now I want to add Avail.

Here is my perl line that grabs capacity. How do I get "Avail"?? Thanks!

my @df = qx (df -k /tmp);
my $cap;
foreach my $df (@df)
        {
       开发者_如何转开发  ($cap) =($df =~ m!(\d+)\%!);
        };

print "$cap\n";


The easy perl way:

perl -MFilesys::Df -e 'print df("/tmp")->{bavail}, "\n"'


This has the merit of producing a nice data structure for you to query all the info about each filesystem.

# column headers to be used as hash keys
my @headers = qw(name size used free capacity mount);

my @df = `df -k`;
shift @df;  # get rid of the header

my %devices;
for my $line (@df) {
    my %info;
    @info{@headers} = split /\s+/, $line;  # note the hash slice
    $info{capacity} = _percentage_to_decimal($info{capacity});
    $devices{ $info{name} } = \%info;
}

# Change 12.3% to .123
sub _percentage_to_decimal {
    my $percentage = shift;
    $percentage =~ s{%}{};
    return $percentage / 100;
}

Now the information for each device is in a hash of hashes.

# Show how much space is free in device /dev/ad4s1e
print $devices{"/dev/ad4s1e"}{free};

This isn't the simplest way to do it, but it is the most generally useful way to work with the df information putting it all in one nice data structure that you can pass around as needed. This is better than slicing it all up into individual variables and its a technique you should get used to.

UPDATE: To get all the devices which have >60% capacity, you'd iterate through all the values in the hash and select those with a capacity greater than 60%. Except capacity is stored as a string like "88%" and that's not useful for comparison. We could strip out the % here, but then we'd be doing that everywhere we want to use it. Its better to normalize your data up front, that makes it easier to work with. Storing formatted data is a red flag. So I've modified the code above which reads from df to change the capacity from 88% to .88.

Now its easier to work with.

for my $info (values %devices) {
    # Skip to the next device if its capacity is not over 60%.
    next unless $info->{capacity} > .60;

    # Print some info about each device
    printf "%s is at %d%% with %dK remaining.\n",
        $info->{name}, $info->{capacity}*100, $info->{free};
}

I chose to use printf here rather than interpolation because it makes it a bit easier to see what the string will look like when output.


Have you tried simply splitting on whitespace and taking the 4th and 5th columns?

my @cols = (split(/\s+/, $_));
my $avail = $cols[3];
my $cap   = $cols[4];

(Fails if you have spaces in your device names of course...)


Us split instead, and get the args from the resulting array. E.g.

my @values = split /\s+/, $df;
my $avail = $values[3];

Or:

($filesystem, $size, $used, $avail, $cap, $mount) = split /\s/, $df;


I think it is probably best to split the lines, skipping the first line. Since you don't mind using @df and $df, neither do I:

my @df = qx(df -k /tmp);
shift @df;                # Lose df heading line
foreach my $df (@df)
{
    my($system, $size, $used, $avail, $capacity, $mount) = split / +/, $df;
    ....
}

This gives you all the fields at once. Now you just need to interpret the 'G' and lose the '%', etc.


foreach my $device ( @df ) {
    next unless $device =~ m{^/};
    my( $filesystem, $size, $used, $avail, $cap, $mounted ) = split /\s+/, $device;
    # you take it from there.... ;)
}


Lots of variations on a theme here. I would keep the first line, since it gives a nice header:

$ perl -E '$,=" "; open my $fh, "-|", "df -k /tmp"; 
  while(<$fh>) { @a=split; say @a[3,4]}'

On second thought, this is a lot cleaner:

$ df -k /tmp | perl -naE '$,="\t"; say @F[3,4]'
Available       Capacity
20862392        92%

Final thought: don't use perl at all:

$ df -h /tmp | tr -s ' ' '\t'  | cut  -f 3,4

or

$ df -h /tmp | awk '{print $3 "\t" $4}'
0

精彩评论

暂无评论...
验证码 换一张
取 消