0

我有一个大文件,我想从中解析和抓取选择的文本片段。这是文件中的实际示例:

en-US   AcceptedText pt="dial:def"Tag u="contact"Mom/Tag/AcceptedText 11373

我想抓取的文本片段在第一个":. 在上面的例子中,它就是这个词dial

这是我整理的脚本:

#!/usr/bin/perl

open (SESAME, '/home/my_name/whereMyFileLives.txt');
while (<SESAME>) {
  $text .= $_;
}
close (SESAME);

$text =~ /\n*$/;
$text =~ m/ \" (.*) :> /;

print $text;

当我运行此脚本时,它会将文件完全按照文件的原样打印到终端。它不解析文本并提取我希望它提取的文本片段..

任何指针?

4

3 回答 3

1
my ($string) = $text =~ /"(.*?):/;
于 2013-07-10T14:13:35.797 回答
1

不明白你为什么要与 进行第一次匹配\n,但对于你的任务,你可以这样做:

my ($result) = $text =~ /\"([^:]*):/;
于 2013-07-10T14:08:55.027 回答
-1

尝试:

#!/usr/bin/env perl

use strict;
use warnings;

# --------------------------------------

use charnames qw( :full :short );
use English qw( -no_match_vars ) ;  # Avoids regex performance penalty

# open (SESAME, '/home/my_name/whereMyFileLives.txt');
#
# Please use the three-argument open
my $sesame_file = '/home/my_name/whereMyFileLives.txt';
open my $sesame_fh, '<', $sesame_file or die "could not open $sesame_file: $OS_ERROR\n";

# while(<SESAME>)
while( my $line = <$sesame_fh> ){

# {
# $text .= $_;
# }
# close (SESAME);
# $text=~/\n*$/;
# $text=~m/ \" (.*) :> /;
# print $text;
#
# No need to store the complete text, just extract what you want from each line
    if( $line =~ m{ \" ( [^:]* ) \: }msx ){
        my $snippet = $1;
        print "$snippet\n";
    } # end if

} # end while
close $sesame_fh or die "could not close $sesame_file: $OS_ERROR\n";
于 2013-07-10T14:22:48.703 回答