首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将数据从文件中提取到另一个文件中

将数据从文件中提取到另一个文件中
EN

Stack Overflow用户
提问于 2014-07-22 10:18:30
回答 3查看 73关注 0票数 1

我想提取一个文件,其中包含一个人的数据信息,如姓名,地址,电话..。该文件可以如下所示:

代码语言:javascript
复制
Name:John
FirstName:Smith
Address:Main Street
Phone:32674632
Name:Alice
FirstName:Meyers
Address:Forth Av.
Phone:273267462

但有时地址写在两行:

代码语言:javascript
复制
Name:John
FirstName:Smith
Address:Main Street
Phone:32674632
Name:Alice
FirstName:Meyers
Address:Forth Av.
street 54
Phone:273267462

我写了这个:

代码语言:javascript
复制
while (<INPUT>) {
        chomp;
        if (/^Name/) {
            ($match) = /Name:(.*)/;
            $string = $string.$match." ";
            next;
        } 
        if (/^FirstName/) {
            ($match) = /FirstName:(.*)/;
            $string = $string.$match." ";
            next;
        } 
        if (/^Address/) {
            ($match) = /Address:(.*)/;
            $string = $string.$match." ";
            next;
        } 
        if (/Address:(.*)$Phone/) {
            ($match) = /Address:(.*)$phone/;
            $string = $string.$match." ";
            next;
        }
        if (/^Phone/) {
            ($match) = /Phone:(.*)/;
            $string = $string.$match." ";
            print OUTPUT "$string\n";
            $string = "";
            next;
        } 
}

有人能帮我找到解决这些问题的方法吗?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2014-07-22 12:00:47

代码语言:javascript
复制
#!/usr/bin/perl -w

use warnings;
use strict;

use Data::Dumper;

my $key = '';
my %person = ();

while (<>) {                            # for each line
    chomp;
    if (/^(.+?):(.*)/) {                # Do we have a key followed by a colon?
        $key = $1;                      # the key is the part before the colon
        $person{$key} = $2;             # The value is the part after the colon
        if ($key eq 'Phone') {          # A line with a 'Phone' key is the last one for a person
            print Dumper(\%person);     # Dump our person
            $key = '';
            %person = ();               # Start a new person
        }
    } else {
        $person{$key} .= " $_";         # No key means just append this line to the value for the previous key
    }
}
票数 0
EN

Stack Overflow用户

发布于 2014-07-22 10:47:44

尝尝这个

代码语言:javascript
复制
if (/^Address/) {
        ($match) = /Address:(.*)$Phone/;
        $string = $string.$match." ";
        next;
    } 

如果您的文件包含相同的格式,即在“地址”之后,下一个记录是“电话”,则编写表达式以搜索地址和电话之间的完整字符串。/^Address:.*$Phone/

然后像这样搜索/^Address.* [\s.*:]$/

以地址开头,以空格结尾任何字符:。一定要起作用。

票数 0
EN

Stack Overflow用户

发布于 2014-07-22 11:06:21

代码语言:javascript
复制
@array=split /\nName/,<INPUT>;
while my $vals (@array) {
    chomp($vals);
    my @spls = ($1,$2,$3,$4) if($_=~/:(.*?)FirstName:(.*?)Address:(.*?)Phone:(.*)/);
    chomp(@spls);
    my $string = join(" ",@spls);
    print OUTPUT "$string\n";          
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24884682

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档