【问题标题】:Add headings to the a text file in perl在 perl 中将标题添加到文本文件
【发布时间】:2014-02-05 02:12:37
【问题描述】:

我有一个文本文件,其中包含以下几行作为示例

This is line
This is line with test
This is ine with 2test
This is line 2
This is line 3
This is line with 3test
This is line 4
This is line with 4test

现在我想要一个代码来更改文本文件如下:

Lines with test
This is line with test
This is ine with 2test
This is line with 3test
This is line with 4test

Lines without test
This is line
This is line 2
This is line 3
This is line 4

我正在使用以下代码。我假设我的代码会打印每一行的标题,但由于一些错误我无法执行代码。 你能帮帮我吗?

#!/usr/bin/perl
use strict;
use warnings;
open(FH, '<filetest.txt');
my @queues = <FH>;
close(FH);
open(OFH,'>testfile.txt');
my $name;
foreach $name(@queues)
{
if($name =~ 'test')
{
print OFH "Lines with test\n";
print OFH $1;
}
else{
print OFH "Lines without test\n";
print OFH $1;
}
close(OFH);
}

注意:我更正了错误以删除语法错误,但仍然没有将任何内容写入文件 testfile.txt

【问题讨论】:

  • “由于一些错误”——确切地说是语法错误。提示:错误的来源可能早于报告错误的位置。还要注意# 开始一行注释,并且正则表达式必须使用某种正则表达式引用,例如/foo/。但即使在那之后,您的代码仍然存在各种错误。 (1) 一次读取和写入同一个文件是行不通的。 (2) 您对open 使用过时的语法,并且不执行任何错误检查。 (3) $1 不会被填充。使用$_。 (4) 你输出 each 行的标题。相反,首先将行放入数组中。 (…)

标签: perl filehandle


【解决方案1】:

试一试:

#!/usr/bin/perl
use strict;
use warnings;

my $infile = 'filetest.txt';
my $outfile = 'testfile.txt';

# use 3-arg open and if open succeeded
open my $fh_in, '<', $infile or die "Unable to open file '$infile' for reading: $!";

my @with_test;
my @without_test;
while (<$fh_in>) {
    if (/test/) {
        push @with_test, $_;
    } else {
        push @without_test, $_;
    }
}
close $fh_in;

open my $fh_out, '>', $outfile or die "Unable to open file '$outile' for writting: $!";
print $fh_out "Lines with test\n";
print $fh_out @with_test;
print $fh_out "Lines without test\n";
print $fh_out @without_test;

close $fh_out;

【讨论】:

  • 感谢 M42。这是有效的。我实际上稍微更改了您的脚本,以便将输出写入 infile 本身。我不需要 infile 的副本。你觉得这样做有什么问题吗?
  • @user3164754:不客气。您可以将同一文件用于输入和输出,因为输入文件在打开输出之前已关闭。但我会在处理之前做一个备份......
【解决方案2】:

我没有测试过这个。这个想法是立即将“with test”行写入文件 “无测试”行被存储(在一个名为“无”的数组中)直到程序结束,然后写入

#!/usr/bin/perl
use strict;
use warnings;
open(FH, '<filetest.txt');
my @queues = <FH>;
close(FH);
open(OFH,'>testfile.txt');
my $name;
my @without=();
foreach $name(@queues)
{
if($name =~ 'test')
{
  print OFH $name;
}
else{
  push @without, $name;
}
print OFH "\n\nlines without\n";
print OFH @without;

}

【讨论】:

  • push 的语法不是这样的,你颠倒了参数的顺序!
【解决方案3】:

您还应该在读取文件句柄之前添加local $/。 看一下这个: how to read the whole file. 匹配格式应该是这样的:if($somestr =~ /#/)。 而且你不应该在循环中关闭文件句柄。

#!/usr/bin/perl

use strict;
use warnings;
local $/;
open(FH, 'file#.txt');
my $s;
$s = <FH>;
close(FH);
open(FH, '>file#.txt');
my @queues = split(/\n/, $s);
my @arr;
my @arr2;
for($s=0; $s<$#queues+1; $s++)
{

    if($queues[$s] =~ /#/)
    {
        push(@arr, $queues[$s]."\n");
    }
    else
    {
        push(@arr2, $queues[$s]."\n");
    }
}
print FH "lines with #\n";
print FH @arr;
print FH "lines without #\n";
print FH @arr2;

【讨论】:

  • 为什么要本地化$/,然后用split 重新实现它的功能?单独留下$/ 并将整个文件读入一个数组要高效得多。另外,为什么要使用(可能有问题的)C 风格的 for 循环而不是 foreach 循环?你也不应该像这样重复使用$s。这个答案中唯一比问题更好的部分是您将元素推入两个数组。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-12-02
  • 2010-10-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-17
相关资源
最近更新 更多