使用perl为cacti制作正确的脚本时遇到了一些麻烦。
这是我可以得到的数据的输出:
Pac max phase 1: 2150W
Pac max phase 2: 0W
Pac max phase 3: 0W
Energy Production:
EToday: 7.282kWh
ETotal: 1113.263kWh
Operation Time: 2763.12h
Feed-In Time : 2376.42h
DC Spot Data:
String 1 Pdc: 1917271.250kW - Udc: 280.00V - Idc: 2.118A
String 2 Pdc: 1934451.875kW - Udc: 7789238.50V - Idc: 878461.750A
SUSyID: 246 - SN: 2002268779
AC Spot Data:
Phase 1 Pac : 1635021.625kW - Uac: 237.10V - Iac: 1852400.000A
Phase 2 Pac : 1730176.375kW - Uac: 0.00V - Iac: 1277195.375A
Phase 3 Pac : 543451.500kW - Uac: 0.00V - Iac: 822742.312A
Total Pac : 0.560kW
我设法通过一个小脚本获得了一些数据。
while(<STDIN>) {
chomp;
if (s/^Pac[ ]max[ ]phase[ ]1[]*[:][ ]*(\d+\.*\d+).*/\1/) {
print "Max:$_ ";
}
if (s/EToday[]*[:][ ]*(\d+\.*\d+).*/\1/) {
print "EToday:$_ ";
}
if (s/ETotal[]*[:][ ]*(\d+\.*\d+).*/\1/) {
print "ETotal:$_ ";
}
在EToday之后我在回归中获得了很多空间我还想获得Idc的价值。 我有麻烦做出正确的输出。 有人可以帮我创建以下输出:
Pac max phase 1:
EToday:
ETotal:
Operation Time:
Feed-In Time :
Udc:
Idc:
Uac:
Iac:
Total Pac :
Grid Freq. :
aaa:xxxx b:xxxx c:xxxx等。
亲切的问候, 马亭
答案 0 :(得分:1)
您没有在您的dample数据中显示Grid Freq.
,但此程序是否可以解决您的问题?
它形成一个正则表达式,匹配您列出的任何标签,后跟一个冒号:
和一个数字,在输入的每一行中打印出类似图案的所有出现。
use strict;
use warnings;
my @fields = (
'Pac max phase 1', 'Pac max phase 2', 'Pac max phase 3',
'EToday', 'ETotal',
'Operation Time', 'Feed-In Time',
'Udc', 'Idc', 'Uac', 'Iac',
'Total Pac',
'Grid Freq.',
);
my $re = join '|', @fields;
my %printed;
while(<DATA>) {
chomp;
while ( /($re)\s*:\s*(\d+(?:\.\d+)?)/g ) {
print "$1: $2\n" unless $printed{$1}++;
}
}
__DATA__
Pac max phase 1: 2150W
Pac max phase 2: 0W
Pac max phase 3: 0W
Energy Production:
EToday: 7.282kWh
ETotal: 1113.263kWh
Operation Time: 2763.12h
Feed-In Time : 2376.42h
DC Spot Data:
String 1 Pdc: 1917271.250kW - Udc: 280.00V - Idc: 2.118A
String 2 Pdc: 1934451.875kW - Udc: 7789238.50V - Idc: 878461.750A
SUSyID: 246 - SN: 2002268779
AC Spot Data:
Phase 1 Pac : 1635021.625kW - Uac: 237.10V - Iac: 1852400.000A
Phase 2 Pac : 1730176.375kW - Uac: 0.00V - Iac: 1277195.375A
Phase 3 Pac : 543451.500kW - Uac: 0.00V - Iac: 822742.312A
Total Pac : 0.560kW
<强>输出强>
Pac max phase 1: 2150
Pac max phase 2: 0
Pac max phase 3: 0
EToday: 7.282
ETotal: 1113.263
Operation Time: 2763.12
Feed-In Time: 2376.42
Udc: 280.00
Idc: 2.118
Uac: 237.10
Iac: 1852400.000
Total Pac: 0.560