我正在尝试将我的torrent程序Deluge的输出转换为我可以放入php文件列中的内容。
deluge-console info 的输出如下所示(示例):
Name: 2014-01-07-wheezy-raspbian.zip
ID: ebd9f05d220e7afd62fcbb10743bf9210a59efd5
State: Seeding Up Speed: 0.0 KiB/s
Seeds: 0 (10) Peers: 0 (0) Availability: 0.00
Size: 780.0 MiB/780.0 MiB Ratio: 17.988
Seed time: 10 days 16:29:52 Active: 10 days 17:44:08
Tracker status: raspberrypi.org: Announce OK
Name: ArchLinuxARM-2014.01-rpi.img.zip
ID: d8b3fb8bf7064108d26390d8a3ec586b70182e18
State: Seeding Up Speed: 0.0 KiB/s
Seeds: 0 (3) Peers: 0 (0) Availability: 0.00
Size: 182.7 MiB/182.7 MiB Ratio: 8.222
Seed time: 10 days 16:05:32 Active: 10 days 16:49:27
Tracker status: raspberrypi.org: Announce OK
我如何使用awk来获得这样的东西:
Name State Ratio Size
2014-01-07-wheezy-raspbian.... Seeding 17.988 780.0 MB
ArchLinuxARM-2014.01-rpi.im... Seeding 8.222 182.7 MB
我知道我必须使用不同的输入和输出变量来实现这一点,但作为awk的新手,我希望得到一些初学者的帮助。
运行命令时,答案 0 :(得分:1)
你可以使用这个awk:
awk -F ': *| +' '$1 ~ /Name|Size|State/{a[$1]=$2}
NF > 1 && $(NF-1) ~ /Ratio/{
printf "%-40s %-15s %10s %10s MB\n", a["Name"], a["State"], $NF, a["Size"];
delete a; next}' file
2014-01-07-wheezy-raspbian.zip Seeding 17.988 780.0 MB
ArchLinuxARM-2014.01-rpi.img.zip Seeding 8.222 182.7 MB
答案 1 :(得分:0)
为什么你不想使用php本身?
<body>
<table border=1>
<tr>
<td>Name</td>
<td>State</td>
<td>Ratio</td>
<td>Size</td>
</tr>
<?php
$output=array();
exec("FULL_PATH_TO_DELUGE_CONSOLE/deluge-console info",$output);
$raw=explode("\n", $output);
foreach ($raw as $row)
{
$resultset=explode(' ',$row);
switch ($resultset[0])
{
case 'Name:':
$name=$resultset[1];
break;
case 'State:':
$state=$resultset[1];
break;
case 'Size:':
$size=$resultset[1];
$ratio=$resultset[5];
echo '<tr><td>'.$name.'</td><td>'.$state.'</td><td>'.$ratio.'</td><td>'.$size.'</td></tr>';
break;
}
}
?>
</table>
</body>
答案 2 :(得分:0)
使用awk
deluge-console info|awk '$1=="Name:" {name=$2} $1=="State:" {state=$2} $1=="Size:" {print name,state,$6,$2}' >output_file
答案 3 :(得分:0)
$ awk -v RS= '{print $2, $6, $24, $20, "MB"}' file
2014-01-07-wheezy-raspbian.zip Seeding 17.988 780.0 MB
ArchLinuxARM-2014.01-rpi.img.zip Seeding 8.222 182.7 MB
使用printf而不是print来格式化。