gpt4 book ai didi

java - 使用正则表达式或unix选择特定列

转载 作者:行者123 更新时间:2023-11-30 04:10:23 26 4
gpt4 key购买 nike

我正在使用 Runtime.getRuntime().exec("df") 来获取分区的空间详细信息。

Filesystem     1K-blocks    Used Available Use% Mounted on
/dev/sdb12 41022792 4219168 34713128 11% /
udev 8103980 4 8103976 1% /dev
tmpfs 3245332 924 3244408 1% /run
none 5120 0 5120 0% /run/lock
none 8113328 160 8113168 1% /run/shm
/dev/sdb2 262144 28584 233560 11% /boot/efi

如何使用正则表达式或 UNIX 命令获取第 2、3 和 4 列作为输出 - 更改标题如下 - 可用内存、已用内存和总内存完全按照此顺序。
所以期望的输出是:

Available    Used     Total
34713128 4219168 41022792
8103976 4 8103980
3244408 924 3245332
...

谢谢!

最佳答案

以下相当长的字符串可以满足您的要求:

df | awk 'BEGIN {print "   Available        Used       Total";} {if (NR>1)  printf("%12d%12d%12d\n", $4, $3, $2);}'

说明:

| awk          take the output of the df command as input to awk
BEGIN do this first: print headers. Note the use of spaces to align things
if (NR > 1) skip "record 1" = do not do anything with the headers
printf() do formatted printing
%12d print integer in fixed width of 12 characters (maintains alignment

我的机器上的结果:

   Available        Used       Total
71644456 551306776 623463232
0 372 372
0 0 0
0 0 0
407510704 377391216 784901920
618400760 549288520 1167689280

没有磁盘名称就不太有用,但这就是您要问的......

关于java - 使用正则表达式或unix选择特定列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19754202/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com