如何使用 Perl 提取和保存文本?

发布于 2024-07-06 15:59:39 字数 606 浏览 5 评论 0原文

没有提取数据输出到data2.txt? 代码出了什么问题?

MyFile.txt

ex1,fx2,xx1
mm1,nn2,gg3
EX1,hh2,ff7

这是我在 data2.txt 中想要的输出:

ex1,fx2,xx1
EX1,hh2,ff7


#! /DATA/PLUG/pvelasco/Softwares/PERLINUX/bin/perl -w

my $infile  ='My1.txt';
my $outfile ='data2.txt';

open IN,  '<', $infile  or die "Cant open $infile:$!";
open OUT, '>', $outfile or die "Cant open $outfile:$!";

while (<IN>) {   
  if (m/EX$HF|ex$HF/) {
    print OUT $_, "\n";      
    print $_;   
  }
}

close IN;
close OUT;

No extracted data output to data2.txt? What goes wrong to the code?

MyFile.txt

ex1,fx2,xx1
mm1,nn2,gg3
EX1,hh2,ff7

This is my desired output in data2.txt:

ex1,fx2,xx1
EX1,hh2,ff7

#! /DATA/PLUG/pvelasco/Softwares/PERLINUX/bin/perl -w

my $infile  ='My1.txt';
my $outfile ='data2.txt';

open IN,  '<', $infile  or die "Cant open $infile:$!";
open OUT, '>', $outfile or die "Cant open $outfile:$!";

while (<IN>) {   
  if (m/EX$HF|ex$HF/) {
    print OUT $_, "\n";      
    print $_;   
  }
}

close IN;
close OUT;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

血之狂魔 2024-07-13 15:59:39

这个正则表达式没有意义:

m/EX$HF|ex$HF/

$HF 应该是一个变量吗? 你想匹配什么?

另外,您编写的每个 Perl 脚本中的第二行应该是:

use strict;

它会让 Perl 捕获此类错误并告诉您它们,而不是默默地忽略它们。

This regex makes no sense:

m/EX$HF|ex$HF/

Is $HF supposed to be a variable? What are you trying to match?

Also, the second line in every Perl script you write should be:

use strict;

It will make Perl catch such mistakes and tell you about them, rather than silently ignoring them.

从﹋此江山别 2024-07-13 15:59:39
while (<IN>) {
  if (m/^(EX|ex)\d.*/) {   
    print OUT "$_";      
    print $_;   
  }
}
while (<IN>) {
  if (m/^(EX|ex)\d.*/) {   
    print OUT "$_";      
    print $_;   
  }
}
缪败 2024-07-13 15:59:39

抱歉,如果这看起来像是在说明明显的出血,但是有什么问题

grep -i ^ex < My1.txt > data2.txt

......或者如果您真的想在 perl 中执行此操作(并且这没有任何问题):

perl -ne '/^ex/i && print' < My1.txt > data2.txt

这假设请求的目的是查找以EX,不区分大小写。

Sorry if this seems like stating the bleeding obvious, but what's wrong with

grep -i ^ex < My1.txt > data2.txt

... or if you really want to do it in perl (and there's nothing wrong with that):

perl -ne '/^ex/i && print' < My1.txt > data2.txt

This assumes the purpose of the request is to find lines that start with EX, with case-insensitivity.

追我者格杀勿论 2024-07-13 15:59:39

当我运行您的代码,但将输入文件命名为 My1.txt 而不是 MyFile.txt 时,我得到了所需的输出 - 空行除外,您可以通过删除来删除空行print 语句中的 , "\n"

When I run your code, but name the input file My1.txt instead of MyFile.txt I get the desired output - except with empty lines, which you can remove by removing the , "\n" from the print statement.

随风而去 2024-07-13 15:59:39

文件名不匹配。

open(my $inhandle, '<', $infile)   or die "Cant open $infile: $!";
open(my $outhandle, '>', $outfile) or die "Cant open $outfile: $!";

while(my $line = <$inhandle>) {   

    # Assumes that ex, Ex, eX, EX all are valid first characters
    if($line =~ m{^ex}i) {         # or   if(lc(substr $line, 0 => 2) eq 'ex') {
        print { $outhandle } $line;      
        print $line;
    }
}

是的,总是总是使用严格;

你也可以chomp $line并且(如果使用perl 5.10)说$line em> 而不是打印“$line\n”

The filenames don't match.

open(my $inhandle, '<', $infile)   or die "Cant open $infile: $!";
open(my $outhandle, '>', $outfile) or die "Cant open $outfile: $!";

while(my $line = <$inhandle>) {   

    # Assumes that ex, Ex, eX, EX all are valid first characters
    if($line =~ m{^ex}i) {         # or   if(lc(substr $line, 0 => 2) eq 'ex') {
        print { $outhandle } $line;      
        print $line;
    }
}

And yes, always always use strict;

You could also chomp $line and (if using perl 5.10) say $line instead of print "$line\n".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文