0

我有一个 txt 文件,其中提供了很多信息。我只想读取和存储“状态”部分。

例子:

id........username...... status......language .......image  

11111 abcdefg Man Utd won for the second time ENG img1244

11112 abcdaaa Man Utd won for the third  time ENG img1245 

11113 abcdbbb Man Utd won for the fourth time ENG img1246

11114 abcdccc Man Utd won for the fifth  time ENG img1247 

11115 abcdddd Man Utd won for the sixth  time ENG img1248 

And what I should obtain is the following

Man Utd won for the second time 

Man Utd won for the third  time 

Man Utd won for the fourth time

Man Utd won for the fifth  time

Man Utd won for the sixth  time

我想要做的是将字符串数据从用户名存储到“ENG”字符串。

谢谢你的帮助。

4

1 回答 1

0

你可以用一个简单的 perl 脚本来做到这一点。对于 windows,perl 可以从activestate下载。Linux 通常已经安装了 perl。

要使用:

  1. 安装(或已经拥有)perl
  2. 将下面的脚本复制到文本文件中
  3. 使用您选择的简单名称和 .pl 扩展名保存文件(例如:parser.pl)
  4. 将源文件保存到同一目录并将其命名为“input.txt”
  5. 从 cmd 窗口执行:perl parser.pl
  6. 脚本的结果将在名为“output.txt”的文件中创建(在同一目录中),如果该文件存在,将被覆盖。

该脚本假定:

  1. 您要查找的文本以 Man 或 Woman 开头
  2. ENG 文本不会出现在您要查找的文本中,仅出现在末尾。
  3. 语言文本始终为 ENG。如果不在第 18 行用 (?:ENG|OTHER1|OTHER2|ETC) 替换 ENG

剧本:

!/usr/local/bin/perl

使用严格;

unless(open(INFILE, "input.txt")){
  print "Unable to open input file input.txt for reading, possible reason: $!\n";
  exit;
};

unless(open(OUTFILE, ">output.txt")){
  print "Unable to open output file output.txt for writing, possible reason: $!\n";
  exit;
};

my $x = 1;
foreach my $line (<INFILE>){
   print "$line";
   if($line =~ /((?:Wom|M)an.*) ENG/){
      print OUTFILE $1."\n";
   }else{
      print "No match found on line $x\n";
   }
   $x++;
}

close(INFILE);
close(OUTFILE);
exit;
于 2013-06-12T17:12:16.690 回答