unix df 上的正则表达式帮助 [英] regex help on unix df

查看:20
本文介绍了unix df 上的正则表达式帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一些帮助来调整我的代码以在这个 unix df 输出中寻找另一个属性:

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

例如

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.

这是我的 perl 行,用于获取容量.我如何获得可用"?谢谢!

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
";

推荐答案

它的优点是可以生成一个很好的数据结构,供您查询有关每个文件系统的所有信息.

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

这不是最简单的方法,但它是处理 df 信息的最普遍有用的方法,将其全部放入一个可以根据需要传递的良好数据结构中.这比将其全部切成单个变量要好,而且您应该习惯这种技术.

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.

更新:要获得容量大于 60% 的所有设备,您需要遍历哈希中的所有值并选择容量大于 60% 的设备.除了容量存储为像88%"这样的字符串,这对比较没有用处.我们可以在此处去掉 %,但随后我们会在任何想要使用它的地方都这样做.最好预先规范化您的数据,这样可以更轻松地使用.存储格式化数据是一个危险信号.所以我修改了上面从 df 读取的代码,将容量从 88% 更改为 .88.

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.

现在更容易使用了.

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.
",
        $info->{name}, $info->{capacity}*100, $info->{free};
}

我在这里选择使用 printf 而不是插值,因为这样可以更容易地看到输出时字符串的样子.

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.

这篇关于unix df 上的正则表达式帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆