1

有人可以帮我吗?

这是我的记录:

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|收盘总额|$236,313.84

4

4 回答 4

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;
}
于 2013-07-05T06:34:19.440 回答
1
while (<>) {
  chomp;
  if (/^{$/ .. /^(})$/) {
    if ($1 || /^$/) {
      print "$a\n";                                                             
      $a = ""; 
    } elsif (!/^{$/) {
      $a .= "|" if ($a == '');
      $a .= $_; 
    }   
  }
}

但是mpapec的要优雅得多:)

于 2013-07-05T06:33:03.137 回答
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
于 2013-07-05T06:37:19.170 回答
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
于 2013-07-05T14:34:42.597 回答