有人能帮助我吗?
这是我的记录:
Client1|fiswt|36214|784423|21 March 2013
Office|Level 9 83 Clarence Street
Transout|Cash|6904.29|$2.32|$16,000.00
closingtot|$236,313.84
Client1|fiswt|36214|784423|21 March 2013
Office|Level 9 83 Clarence Street
Transout|Cash|6904.29|$2.32|$16,000.00
closingtot|$236,313.84
Client1|fiswt|36214|784423|21 March 2013
Office|Level 9 83 Clarence Street
Transout|Cash|6904.29|$2.32|$16,000.00
closingtot|$236,313.84
这是我想要的示例输出。
Client1|fiswt|36214|784423|21 March 2013|Office|Level 9 83 Clarence Street|Transout|Cash|6904.29|$2.32|$16,000.00|closingtot|$236,313.84
Client1|fiswt|36214|784423|21 March 2013|Office|Level 9 83 Clarence Street|Transout|Cash|6904.29|$2.32|$16,000.00|closingtot|$236,313.84
Client1|fiswt|36214|784423|21 March 2013|Office|Level 9 83 Clarence Street|Transout|Cash|6904.29|$2.32|$16,000.00|closingtot|$236,313.84
我实际上有这个解决方案:
chomp($line) unless($line =~ /^Closing/i);
但是每行末尾的输出存在一些问题,应该添加管道(|):
像这样$ 16,000.00 | closingtot | $ 236,313.84
答案 0 :(得分:4)
perl -00ne 's|\s+$||; s# [\r\n]+ #|#xg; print $_, "\n";' input_file
-00
启用paragraph mode
,其中文件中的记录由两个或更多新行分隔。
s|\s+$||
删除所有尾随空格,s# [\r\n]+ #|#xg
用|
替换剩余的换行符
更换一个衬垫:
sub read_from_file {
local $/ = "";
open my $fh, "input_file" or die $!;
while (<$fh>) {
s|\s+$||;
s# [\r\n]+ #|#xg;
print $_, "\n";
}
close $fh;
}
答案 1 :(得分:1)
while (<>) {
chomp;
if (/^{$/ .. /^(})$/) {
if ($1 || /^$/) {
print "$a\n";
$a = "";
} elsif (!/^{$/) {
$a .= "|" if ($a == '');
$a .= $_;
}
}
}
但是mpapec更优雅:)
答案 2 :(得分:1)
这是一个适合您需求的脚本:
use strict;
use warnings;
my $line = '';
while(<DATA>) {
if( /^\S/ ) {
chomp;
$line .= '|' if $line ne '';
$line .= $_
}
else {
print $line, "\n";
$line = '';
}
}
print $line, "\n" if($line);
__END__
Client1|fiswt|36214|784423|21 March 2013
Office|Level 9 83 Clarence Street
Transout|Cash|6904.29|$2.32|$16,000.00
closingtot|$236,313.84
Client1|fiswt|36214|784423|21 March 2013
Office|Level 9 83 Clarence Street
Transout|Cash|6904.29|$2.32|$16,000.00
closingtot|$236,313.84
Client1|fiswt|36214|784423|21 March 2013
Office|Level 9 83 Clarence Street
Transout|Cash|6904.29|$2.32|$16,000.00
closingtot|$236,313.84
答案 3 :(得分:0)
将您的数据作为段落读取,并用字段分隔符替换空格。
#!/usr/bin/env perl
use strict;
use warnings;
# --------------------------------------
# Modules
use English qw( -no_match_vars );
# --------------------------------------
# Name: read_paragraph
# Usage: $paragraph = read_paragraph( \*fh );
# Purpose: Read the next paragraph from the file handle
# Parameters: \*fh -- The file handle to read from
# Returns: $paragraph -- the paragraph
#
sub read_paragraph {
my $fh = shift @_;
local $INPUT_RECORD_SEPARATOR = "\n\n";
my $paragraph = readline( $fh );
return $paragraph;
}
# --------------------------------------
# Main
while( my $para = read_paragraph( \*DATA )){
$para =~ s{ \s+ }{|}gmsx;
$para =~ s{ (?> \| )+ \z }{}msx;
print "$para\n";
}
__DATA__
Client1|fiswt|36214|784423|21 March 2013
Office|Level 9 83 Clarence Street
Transout|Cash|6904.29|$2.32|$16,000.00
closingtot|$236,313.84
Client1|fiswt|36214|784423|21 March 2013
Office|Level 9 83 Clarence Street
Transout|Cash|6904.29|$2.32|$16,000.00
closingtot|$236,313.84
Client1|fiswt|36214|784423|21 March 2013
Office|Level 9 83 Clarence Street
Transout|Cash|6904.29|$2.32|$16,000.00
closingtot|$236,313.84