Perl的 - 读取文件的内容结构化的方式 [英] Perl - Reading the contents of a file in a structured manner

查看:139
本文介绍了Perl的 - 读取文件的内容结构化的方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如标题状态,我想读的是动态的Perl脚本生成的现有文件的内容。这个文件将或多或少遵循相同的结构如下面的例子所示。唯一的区别将是如下的数据量。

As the title states, I would like to read the contents of an already existing file that is dynamically generated by the Perl script. This file will more or less follow the same structure as seen in the example below. The only real difference would be the amount of data that follows.

Recv-Q Send-Q             Local Address:Port               Peer Address:Port 
0      128                            *:111                           *:*     
0      128                            *:22                            *:*     
0      128                    127.0.0.1:631                           *:*     
0      128                            *:53944                         *:*     
0      100                    127.0.0.1:25                            *:*   

在读取该文件的目标是使用数据和进一步筛选它的内容。一个例子是,以具有端口只列出数据:22 并present它给最终用户,但是这超出我在这里的问题

The goal with reading this file will be to use the data and further filter the contents of it. An example would be to only list data that has the Port:22 and present it to the end user, but this is beyond my question here.

我想实现的是阅读每个头,如:的recv-Q 发送-Q 本地地址:端口的和的通信地址:端口的为以散列。该值随后将由驻留在每个标题栏下的数据。目标是能够使用的标头,如一个指针值来滤除某些列

What I would like to achieve is to read each header, e.g Recv-Q, Send-Q, Local Address:Port and Peer Address:Port as keys to a hash. The values thereafter will consist of the data residing under each header column. The goal is to be able to filter out certain columns using the header as a pointer to the values.

在code strucutre,我会希望实现将是一个,列出所有的头,每个头中有会说谎的阵列包含所有的列数据。

The code strucutre that I would wish to achieve would be a hash listing all the headers, and within each header there would lie an array contain all the column data.

我没有的我如何将着手解决这个问题的任何code的例子,但我对这个问题的任何帮助,非常AP preciative。

I do not have any code examples of how I would go about to resolve this issue but I am very appreciative of any help on the matter.

推荐答案

只要有从未任何空白字符内的任何你可以使用字段拆分

As long as there are never any whitespace characters inside any of the fields you can just use split

此程序使用正则表达式做了一个非常类似的事情,但划分线分为六个领域,而不是四个 - 保留的端口号与地址分开。它至少应该让你开始

This program uses a regex to do a very similar thing but divides the lines into six fields instead of four — keeping the port numbers separate from the addresses. It should at least get you started

use strict;
use warnings;
use 5.010;

<DATA>; # Lose header line

my @data;
while ( <DATA> ) {
  push @data, [ /[^\s:]+/g ];
}

use Data::Dump;
dd \@data;

__DATA__
Recv-Q Send-Q             Local Address:Port               Peer Address:Port 
0      128                            *:111                           *:*     
0      128                            *:22                            *:*     
0      128                    127.0.0.1:631                           *:*     
0      128                            *:53944                         *:*     
0      100                    127.0.0.1:25                            *:*   

输出

[
  [0, 128, "*", 111, "*", "*"],
  [0, 128, "*", 22, "*", "*"],
  [0, 128, "127.0.0.1", 631, "*", "*"],
  [0, 128, "*", 53944, "*", "*"],
  [0, 100, "127.0.0.1", 25, "*", "*"],
]

这篇关于Perl的 - 读取文件的内容结构化的方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆