从打印输出中删除逗号

时间:2018-03-16 20:47:09

标签: regex perl

grep中的替换会在输出中产生额外的逗号。

open(READ,"<","C:/Users/lab/voice_port.txt");
my @file=<READ>;
close(READ);

my @device_name=grep(s/^(\S+)#.*\n$/$1/,@file);
my @port_number=grep(s/Foreign Exchange Station\s(\d{1,2}\/\d{1,2}\/?\d{0,2})\s.*\n$/$1/,@file);
my @port_status=grep(s/.*Administrative State is\s(\S+).*\n$/$1/,@file);
my @number, @device_type=grep(s/.*Station-id name\s(.*)\sStation-id number\s(\+\d+)\n$/$2,$1/,@file);

打印值:

for my $x(0..$#port_number)
{
print $device_name[0].",".$port_number[$x].",".$port_status[$x].",".$number[$x].",".$device_type[$x]."\n";
}

结果:

Router1,0/1/1,UP,,+3255413655555,MKD, Reproc
Router1,0/1/2,DOWN,,+3289791148755632,Fax Unbek.
Router1,0/1/3,UP,,+3217825478220844,Kitfax Ler

如果在&#34; for&#34;中删除了端口状态和号码之间的逗号。循环,它看起来像预期的(没有额外的逗号)。有没有办法在grep函数中删除这个额外的逗号?

预期结果:

Router1,0/1/1,UP,+3255413655555,MKD, Reproc
Router1,0/1/2,DOWN,+3289791148755632,Fax Unbek.
Router1,0/1/3,UP,+3217825478220844,Kitfax Ler

输入文件:

#
#
Router1# show voice port 
Foreign Exchange Station 0/1/1 Slot is 1, Sub-unit is 1, Port is 1
Operation State is DORMANT
Administrative State is UP
Station-id name MKD, Reproc Station-id number +3255413655555
Digit Duration Timing is set to 100 ms
.
.

1 个答案:

答案 0 :(得分:0)

这一行:

my @number, @device_type
  =grep(s/.*Station-id name\s(.*)\sStation-id number\s(\+\d+)\n$/$2,$1/,@file);

...应该改成两个greps,因为你不能像你尝试的那样一次设置两个数组:

my@number =grep(s/.*Station-id name\s(.*)\sStation-id number\s(\+\d+)\n$/$2/,@file);
my@devtype=grep(s/.*Station-id name\s(.*)\sStation-id number\s(\+\d+)\n$/$1/,@file);
相关问题