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

查看:135
本文介绍了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

到目前为止,我可以提取容量,但是现在我想添加Avail.

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

这不是最简单的方法,但它是处理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.\n",
        $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天全站免登陆