Home: Need a Custom or Prewritten Perl Program?: I need a program that...:
Remove new line character



arnarisetty
New User

Jan 17, 2014, 4:39 PM


Views: 14556
Remove new line character

I have a pipe delimiter file and each row has 10 "|" separators. some times there are new line characters and this messes up the file. Can some one please provide me a simple perl script to count "|" delimiters and if the count is less than 10 then remove new line character and merge next line and validate if the count of delimiters are 10


Laurent_R
Veteran / Moderator

Jan 18, 2014, 4:02 PM


Views: 14500
Re: [arnarisetty] Remove new line character

Maybe something along the lines of this untested piece of code:


Code
while (my $line = <$INPUT>) { 
while ($line !~ /^([^|]*\|){10}/) {
$line .= <$INPUT>;
}
# do something with $line
}


or possibly:

Code
while (my $line = <$INPUT>) { 
$line .= <$INPUT> while ($line =~ s/\|/\|/g) < 10;
# do something with $line
}


Again, this is untested, I *think* both ideas should more or less work, but there may be some mistakes.

If it does not work and if you need further help, please provide some input data to make testing possible.


(This post was edited by Laurent_R on Jan 18, 2014, 4:03 PM)


BillKSmith
Veteran

Jan 19, 2014, 7:55 AM


Views: 14430
Re: [Laurent_R] Remove new line character

Here is a variariation on Laurent_R's algorithn, that includes error echecking.


Code
use strict; 
use warnings;
my $SEP = qr/[|]/;
my $NON_SEP = qr/[^|]/;
while (<DATA>) {
if (m/ (:? $NON_SEP* $SEP ){11,}? /xms) {
die "Illegal record\n";
}
if (/^ (:? $NON_SEP* $SEP ){0,9} $/xms) {
chomp;
$_ .= do {
die "Last record is incomplete\n" if eof;
<DATA>
};
redo;
}
print;
}
__DATA__
1||||||||||
2||||||||||
3|||||
|||||
4||||||||||
5|||||||||


Output:

Code
1|||||||||| 
2||||||||||
3||||||||||
4||||||||||
Last record is incomplete

Good Luck,
Bill