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

試す:

#!/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 に答える