Gossamer Forum
Home : General : Perl Programming :

Get rid of newlines, ONLY if they exist on a certain line?

Quote Reply
Get rid of newlines, ONLY if they exist on a certain line?
Hi. Had a look around, but couldn't find anything related to this :/

Basically, I have a text file, consisting of;

Code:
test|field|123455|
address1|
address2|
address3|
another line|field|123455|
address1|
address2|
address3|

I need to get it to look like;

test|field|123455|address1|address2|address3|
anotherline|field|123455|address1|address2|address3|

Anyone got any suggestions as how I could do this? Unsure I just don't seem to be making any progress :(

TIA

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Get rid of newlines, ONLY if they exist on a certain line? In reply to
try this:
Code:
open (IN, "test.txt");
open (OUT, ">test2.txt");
while (<IN>) {
my $line = $_;
chomp($line);
for (1 .. 3) {
$line .= <IN>;
chomp ($line);
}
print OUT $line . "\n";
}
close OUT;
close IN;

Philip
------------------
Limecat is not pleased.
Quote Reply
Re: [fuzzy logic] Get rid of newlines, ONLY if they exist on a certain line? In reply to
Thanks for the attempt. Unfortunatly, the example I gave wasn't really that accurate. The number of lines that need newlines on changes each time .... I've given up on this for now. Will probably try and tackle it tomorrow when my head is a bit fresher :)

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Get rid of newlines, ONLY if they exist on a certain line? In reply to
You need a regex that looks for multiple instances of | on a line. If it finds more than one | on that line, then add a line break after it.

Is that what you want?

- wil
Quote Reply
Re: [Andy] Get rid of newlines, ONLY if they exist on a certain line? In reply to
Is it simply a multirecord line followed by "x" number of single record lines?

Philip
------------------
Limecat is not pleased.
Quote Reply
Re: [fuzzy logic] Get rid of newlines, ONLY if they exist on a certain line? In reply to
Basically, its a dump file from a client. They send it to me, but it has newlines in it. My current importing script doesn't handle this, as it uses a WHILE, which takes each newline as an entry. I'm basically trying to write some cleanup code, so I can get it all on one line Unsure

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Get rid of newlines, ONLY if they exist on a certain line? In reply to
Code:
open (IN, "test.txt");
open (OUT, ">test2.txt");
my $first_rec = 1;
while (my $rec = <IN>) {
chomp $rec;
if ($rec =~ /.*\|.*\|/) {
print OUT "\n" if $first_rec-- != 1;
}
print OUT $rec;
}
close OUT;
close IN;

Philip
------------------
Limecat is not pleased.