Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6016447
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:59:01+00:00 2026-05-23T02:59:01+00:00

I need some help tweaking my code to look for another attribute in this

  • 0

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";
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T02:59:01+00:00Added an answer on May 23, 2026 at 2:59 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need some help tweaking this php code to print out a default value
I've gone through most of the example code and I still need some help.
Need some help to solve this. I have a gridview and inside the gridview
I need some help to implement this roll over / out effect. This is
I need some help with this procedure: What its supposed to do is try
I need some help creating this extension method. My view inherits from <%@ Page
I need some help from the shell-script gurus out there. I have a .txt
I need some help regarding algorithm for randomness. So Problem is. There are 50
I need some help calculating Pi. I am trying to write a python program
I need some help with jQuery script again :-) Just trying to play with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.