CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

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

Post #1 of 3 (9425 views)
Remove new line character Can't Post

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

Post #2 of 3 (9369 views)
Re: [arnarisetty] Remove new line character [In reply to] Can't Post

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

Post #3 of 3 (9299 views)
Re: [Laurent_R] Remove new line character [In reply to] Can't Post

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

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives