Perl Text :: CSV :: Slurp处理固定宽度输入(Perl Text::CSV::Slurp process the fixed width input)

编程入门 行业动态 更新时间:2024-10-26 00:28:32
Perl Text :: CSV :: Slurp处理固定宽度输入(Perl Text::CSV::Slurp process the fixed width input)

我想将一些文本解析成一个表格,如下所示:

Protocol Address Age (min) Hardware Addr Type Interface Internet 10.35.195.1 - 0024.978a.d2d0 ARPA FastEthernet0/0 Internet 10.35.195.2 73 0002.16a3.9e40 ARPA FastEthernet0/0 Internet 10.35.195.12 130 0007.0e5b.861a ARPA FastEthernet0/0 Internet 10.35.195.14 1 000b.cdc9.7d11 ARPA FastEthernet0/0 Internet 10.35.195.15 3 0021.5a7b.f2af ARPA FastEthernet0/0 Internet 10.35.195.16 0 000c.2909.2298 ARPA FastEthernet0/0 Internet 10.35.195.17 112 0001.e6a2.5a90 ARPA FastEthernet0/0 Internet 10.35.195.24 168 0050.564b.ebd4 ARPA FastEthernet0/0

有固定宽度的文本输入。 一些Params,例如“Hardware Addr”,中有空格。 首先,我使用Text :: CSV :: Slurp,很难定义分隔符。 所以我放弃了。

只是想知道,是否有一些perl模块或嵌入式perl命令(unpack,substr)可以平滑有效地处理这个输入?

I would like to parse the some text into a table which showing below:

Protocol Address Age (min) Hardware Addr Type Interface Internet 10.35.195.1 - 0024.978a.d2d0 ARPA FastEthernet0/0 Internet 10.35.195.2 73 0002.16a3.9e40 ARPA FastEthernet0/0 Internet 10.35.195.12 130 0007.0e5b.861a ARPA FastEthernet0/0 Internet 10.35.195.14 1 000b.cdc9.7d11 ARPA FastEthernet0/0 Internet 10.35.195.15 3 0021.5a7b.f2af ARPA FastEthernet0/0 Internet 10.35.195.16 0 000c.2909.2298 ARPA FastEthernet0/0 Internet 10.35.195.17 112 0001.e6a2.5a90 ARPA FastEthernet0/0 Internet 10.35.195.24 168 0050.564b.ebd4 ARPA FastEthernet0/0

There are text inputs with fixed width. Some Params ,such as "Hardware Addr", have whitespace in it. At the first, I use Text::CSV::Slurp, it is difficult to define separator. So i give up.

Just like to know, are there some perl modules or embedded perl command (unpack, substr)can process this input smoothly and efficiently?

最满意答案

我使用Parse :: FixedLength模块,它可以正确处理这类问题。 这是一个例子:

use strict; use warnings; use Parse::FixedLength; #define your format in the constructor my $pfl = Parse::FixedLength->new([qw(Protocol:10 Addr:34)], {trim=>1}); open my $file, '<', 'file_to_be_readed.txt' or die $!; <$file> #if your file has a header, forget it while( my $line = <$file> ) { my $data = $pfl->parse($line); my $protocol = $data->{Protocol}; my $addr = $data->{Addr}; #... } close $file;

I'd use Parse::FixedLength module, which handles properly this kind of problems. This is an example:

use strict; use warnings; use Parse::FixedLength; #define your format in the constructor my $pfl = Parse::FixedLength->new([qw(Protocol:10 Addr:34)], {trim=>1}); open my $file, '<', 'file_to_be_readed.txt' or die $!; <$file> #if your file has a header, forget it while( my $line = <$file> ) { my $data = $pfl->parse($line); my $protocol = $data->{Protocol}; my $addr = $data->{Addr}; #... } close $file;

更多推荐

本文发布于:2023-07-26 00:11:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1268309.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:宽度   CSV   Slurp   Perl   Text

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!