Reputation: 309
Can someone help me out?
Here's my record:
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
Here's the sample output that I wanted to have.
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
I actually have this Solution:
chomp($line) unless($line =~ /^Closing/i);
but the there is some problem with the output at the end of each line a pipe(|) should be added:
like this
$16,000.00|closingtot|$236,313.84
Upvotes: 1
Views: 4821
Reputation: 50637
perl -00ne 's|\s+$||; s# [\r\n]+ #|#xg; print $_, "\n";' input_file
-00
turns on paragraph mode
where records from file are separated by two or more new lines.
s|\s+$||
removes any trailing white spaces, and s# [\r\n]+ #|#xg
replaces remaining newlines with |
Replacement for one liner:
sub read_from_file {
local $/ = "";
open my $fh, "input_file" or die $!;
while (<$fh>) {
s|\s+$||;
s# [\r\n]+ #|#xg;
print $_, "\n";
}
close $fh;
}
Upvotes: 4
Reputation: 3601
Read your data as paragraphs and replace the spaces with your field separator.
#!/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
Upvotes: 0
Reputation: 13792
This is a script that suits your needs:
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
Upvotes: 1
Reputation: 198324
while (<>) {
chomp;
if (/^{$/ .. /^(})$/) {
if ($1 || /^$/) {
print "$a\n";
$a = "";
} elsif (!/^{$/) {
$a .= "|" if ($a == '');
$a .= $_;
}
}
}
But mpapec's is quite a bit more elegant :)
Upvotes: 1