regex help on unix df

前端 未结 7 1846
耶瑟儿~
耶瑟儿~ 2020-12-02 01:22

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

Ex.

Filesystem     Size    Used   Avail Capacity         


        
相关标签:
7条回答
  • 2020-12-02 01:42

    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;
    
    0 讨论(0)
  • 2020-12-02 01:45

    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.

    0 讨论(0)
  • 2020-12-02 01:46

    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 讨论(0)
  • 2020-12-02 01:48
    foreach my $device ( @df ) {
        next unless $device =~ m{^/};
        my( $filesystem, $size, $used, $avail, $cap, $mounted ) = split /\s+/, $device;
        # you take it from there.... ;)
    }
    
    0 讨论(0)
  • 2020-12-02 01:54

    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...)

    0 讨论(0)
  • 2020-12-02 02:06

    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 讨论(0)
提交回复
热议问题