我有一个名为mail.txt的文件,其中的行打印如下,我想把所有这些行放到一行中,如
由于
这是输入
q2VDWKkY010407 2221878 Sat Mar 31 19:37 <Mailer-daemon> (host map: lookup (my.local.domain): deferred) <yagyavalkbhatt@yahoo.com> <ygyalkatt@yahoo.com> q2VDWKkY010407 2221878 Sat Mar 31 19:37 <Mailer-daemon> (host map: lookup (my.local.domain): deferred) <yagyavalkbhatt@yahoo.com> <yagyav@yahoo.com>
这是输出
q2VDWKkY010407 2221878 Sat Mar 31 19:37 <Mailer-daemon>,(host map: lookup (my.local.domain): deferred),<yagyavalkbhatt@yahoo.com>,<ygyalkatt@yahoo.com> q2VDWKkY010407 2221878 Sat Mar 31 19:37 <Mailer-daemon>,(host map: lookup (my.local.domain): deferred), <yagyavalkbhatt@yahoo.com>,<yagyav@yahoo.com>
答案 0 :(得分:2)
那么你为什么不这样做呢?
open(my $fh, "<", $input_filename);
my @lines = map { chomp; $_} <$fh>; #1
close $fh;
open(my $out, ">", $output_filename);
print $out join "", @lines; # or maybe a different separator, like ","
close $out;
#that's it
注意:如果您想摆脱输入行开头和结尾的额外空格,可以用
替换行#1
my @lines = map { s/\s+$//; s/^\s+//; $_} <$fh>;
答案 1 :(得分:2)
您似乎想在连接的行和记录之间的空白行之间引入逗号分隔符。
下面的代码将带有前导空格的行视为连续行。我们将前导空格和尾随空格分开并将记录粘合在一起。
#! /usr/bin/env perl
use strict;
use warnings;
*ARGV = *DATA; # for demo only
my $line;
while (<>) {
s/\s+$//;
if (s/^\s+//) {
$line .= "," . $_;
next;
}
else {
print $line, "\n\n" if defined $line;
$line = $_;
}
}
print $line, "\n" if defined $line;
__DATA__
q2VDWKkY010407 2221878 Sat Mar 31 19:37 <Mailer-daemon>
(host map: lookup (my.local.domain): deferred)
<yagyavalkbhatt@yahoo.com>
<ygyalkatt@yahoo.com>
q2VDWKkY010407 2221878 Sat Mar 31 19:37 <Mailer-daemon>
(host map: lookup (my.local.domain): deferred)
<yagyavalkbhatt@yahoo.com>
<yagyav@yahoo.com>
输出:
q2VDWKkY010407 2221878 Sat Mar 31 19:37 <Mailer-daemon>,(host map: lookup (my.local.domain): deferred),<yagyavalkbhatt@yahoo.com>,<ygyalkatt@yahoo.com> q2VDWKkY010407 2221878 Sat Mar 31 19:37 <Mailer-daemon>,(host map: lookup (my.local.domain): deferred),<yagyavalkbhatt@yahoo.com>,<yagyav@yahoo.com>
上面的代码包含自己的输入。要在实际数据上使用它,请使用# for demo only
注释和整个__DATA__
部分远程处理该行。然后你可以像在
$ join-lines mail-log
甚至
$ join-lines mail-log1 mail-log2 mail-log3
要将标准输出重定向到文件oneline.log
,请将其作为
$ join-lines mail-log >oneline.log
答案 2 :(得分:2)
如果可以安全地假设以空格开头的行是连续行,则可以通过在全局字符串变量中累积每个复合记录来非常简单地完成此操作。
该程序可以满足要求。 s/^\s+//
语句都删除前导空格并确定该行是否为续行。
use strict;
use warnings;
my $line = '';
while (<DATA>) {
s/\s+\z//;
if (s/^\s+//) {
$line .= ','.$_;
}
else {
print $line, "\n" if $line;
$line = $_;
}
}
print $line, "\n";
__DATA__
q2VDWKkY010407 2221878 Sat Mar 31 19:37 <Mailer-daemon>
(host map: lookup (my.local.domain): deferred)
<yagyavalkbhatt@yahoo.com>
<ygyalkatt@yahoo.com>
q2VDWKkY010407 2221878 Sat Mar 31 19:37 <Mailer-daemon>
(host map: lookup (my.local.domain): deferred)
<yagyavalkbhatt@yahoo.com>
<yagyav@yahoo.com>
<强>输出强>
q2VDWKkY010407 2221878 Sat Mar 31 19:37 <Mailer-daemon>,(host map: lookup (my.local.domain): deferred),<yagyavalkbhatt@yahoo.com>,<ygyalkatt@yahoo.com>
q2VDWKkY010407 2221878 Sat Mar 31 19:37 <Mailer-daemon>,(host map: lookup (my.local.domain): deferred),<yagyavalkbhatt@yahoo.com>,<yagyav@yahoo.com>
答案 3 :(得分:-1)
我想出了这个:
#!usr/bin/perl
my $line;
my $i = 0;
open (FILE1, "<input.txt") or die "Can't find file";
open (FILE2, ">output.txt") or die $!;
while($line = <FILE1>){
if ($line =~ /<Mailer-daemon>/)
{
#If it contains <Mailer-daemon> it retains its normal formatting./\
print FILE2 substr($line, 0 , $line.length()-1); #chops off newline character
$i++;
}
else
{
$line =~ s/\s//g; #this regex kills all whitespace...not sutiable for mailer daemon lines.
print FILE2 $line;
$i++;
}
if ($i == 4)
{
#Every 4th line you want two newline characters as per sample output
print FILE2 "\n\n";
$i = 0;
}
else
{
#comma seperator between non fourth-line parts
print FILE2 ", "
}
}
close FILE1;
close FILE2;
这与您提供的输入和输出非常相似。如果格式稍微改变,我就不会运行它。