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: Perl Programming Help: Beginner:
split up a dump txt file...

 



ptyo
Novice

Oct 7, 2009, 11:45 AM

Post #1 of 7 (485 views)
split up a dump txt file... Can't Post

okay here is what the data looks like in notepad...

BLYTHEISLANDHWY:K8,9,1,1 CHARLESTONST:G14,1,4,7 DEMEREROUNDABOUTCONN:L13,2,* FIDDLERSCV:I7,11,3,3 GOLDENISLESPKWY:J10,7,1,1 HILLPL:K10,103,11,902 KNOXDR:I10,5,1,1 MCKENZIEDR:H8,11,3,3 OLD99RD:H6,8,3,3 PLANTINGHAMMOCKBLVD:M7,9,6* SAVANNAHEVERETTAV:D5,8,3,* STUDANIELDR:I8,11,3,3 VOGELRD: ,5,1,5

what i need is for it to look like.

BLYTHEISLANDHWY:K8,9,1,1
CHARLESTONST:G14,1,4,7
DEMEREROUNDABOUTCONN:L13,2,*
FIDDLERSCV:I7,11,3,3
GOLDENISLESPKWY:J10,7,1,1
HILLPL:K10,103,11,902



basically when I hit at least two capital letters in a row that is a new line.

thanks.


byuecs
New User

Oct 7, 2009, 2:35 PM

Post #2 of 7 (473 views)
Re: [ptyo] split up a dump txt file... [In reply to] Can't Post

I wrote a script for you that does what you want, but there is an issue with your "two capital letters in a row" idea. OLD99RD has 2 numbers that make RD the next token. Here's the script.

Code
#! /usr/bin/perl 

use warnings;
use strict;

my $fileName = "./random.txt"; # put file to pull data from here
my @finalArray = ();
my $tempString = "";

# read in file
open (FILEHANDLE, $fileName) or die "Error: $!";
# put in @data array
my @data = <FILEHANDLE>;
close (FILEHANDLE) or die "Error: $!";
# loop through each line of the file
foreach my $line (@data) {
# split the string (line) according to delimiters and put into array @stuff
my @stuff = split(/([A-Z]{2,})/, $line);
my $i = 0;
# loop through array to print to screen
for ($i=0;$i<scalar @stuff;$i++) {
if ($i % 2 == 0 && $i != 0 && $i != (scalar @stuff)-1) {
print $tempString . $stuff[$i] . "\n";
}
else {
$tempString = $stuff[$i];
}
}
}


And here's the output:

Code
BLYTHEISLANDHWY:K8,9,1,1  
CHARLESTONST:G14,1,4,7
DEMEREROUNDABOUTCONN:L13,2,*
FIDDLERSCV:I7,11,3,3
GOLDENISLESPKWY:J10,7,1,1
HILLPL:K10,103,11,902
KNOXDR:I10,5,1,1
MCKENZIEDR:H8,11,3,3
OLD99
RD:H6,8,3,3
PLANTINGHAMMOCKBLVD:M7,9,6*
SAVANNAHEVERETTAV:D5,8,3,*
STUDANIELDR:I8,11,3,3



ichi
User

Oct 8, 2009, 9:46 PM

Post #3 of 7 (458 views)
Re: [ptyo] split up a dump txt file... [In reply to] Can't Post


In Reply To

basically when I hit at least two capital letters in a row that is a new line.

thanks.



Code
# perl -ne "s/\s+([A-Z]{1,2})/\n\1/g;print" file 
BLYTHEISLANDHWY:K8,9,1,1
CcARLESTONST:G14,1,4,7
DEMEREROUNDABOUTCONN:L13,2,*
FIDDLERSCV:I7,11,3,3
GOLDENISLESPKWY:J10,7,1,1
HILLPL:K10,103,11,902
KNOXDR:I10,5,1,1
MCKENZIEDR:H8,11,3,3
OLD99RD:H6,8,3,3
PLANTINGHAMMOCKBLVD:M7,9,6*
SAVANNAHEVERETTAV:D5,8,3,*
STUDANIELDR:I8,11,3,3
VOGELRD: ,5,1,5



savo
User

Oct 9, 2009, 5:43 AM

Post #4 of 7 (454 views)
Re: [ichi] split up a dump txt file... [In reply to] Can't Post

Here is mine

while (<TEST>) {
s/ (?=[A-Z])/\n/g;
print;
}


shawnhcorey
Enthusiast


Oct 9, 2009, 6:16 AM

Post #5 of 7 (452 views)
Re: [ptyo] split up a dump txt file... [In reply to] Can't Post


In Reply To
okay here is what the data looks like in notepad...

BLYTHEISLANDHWY:K8,9,1,1 CHARLESTONST:G14,1,4,7 DEMEREROUNDABOUTCONN:L13,2,* FIDDLERSCV:I7,11,3,3 GOLDENISLESPKWY:J10,7,1,1 HILLPL:K10,103,11,902 KNOXDR:I10,5,1,1 MCKENZIEDR:H8,11,3,3 OLD99RD:H6,8,3,3 PLANTINGHAMMOCKBLVD:M7,9,6* SAVANNAHEVERETTAV:D5,8,3,* STUDANIELDR:I8,11,3,3 VOGELRD: ,5,1,5

what i need is for it to look like.

BLYTHEISLANDHWY:K8,9,1,1
CHARLESTONST:G14,1,4,7
DEMEREROUNDABOUTCONN:L13,2,*
FIDDLERSCV:I7,11,3,3
GOLDENISLESPKWY:J10,7,1,1
HILLPL:K10,103,11,902



basically when I hit at least two capital letters in a row that is a new line.

thanks.


Are you sure? It looks to me like you want to split on white space.

__END__

I love Perl; it's the only language where you can bless your thingy.

Perl documentation is available at perldoc.perl.org. The list of standard modules and pragmatics is available in perlmodlib.

Get Markup Help. Please note the markup tag of "code".


savo
User

Oct 9, 2009, 6:22 AM

Post #6 of 7 (451 views)
Re: [shawnhcorey] split up a dump txt file... [In reply to] Can't Post

I thought that and tried it but the last one has a space so it needs to be space then capital letter.

VOGELRD: ,5,1,5


shawnhcorey
Enthusiast


Oct 9, 2009, 6:49 AM

Post #7 of 7 (446 views)
Re: [savo] split up a dump txt file... [In reply to] Can't Post


In Reply To
I thought that and tried it but the last one has a space so it needs to be space then capital letter.

VOGELRD: ,5,1,5


Perhaps something like this:

Code
#!/usr/bin/env perl 

use strict;
use warnings;

while( <DATA> ){
chomp;
my @items = split m{ \s+ (?= [[:upper:]] ) }msx, $_;
print join( "\n", @items ), "\n";
}

__DATA__
BLYTHEISLANDHWY:K8,9,1,1 CHARLESTONST:G14,1,4,7 DEMEREROUNDABOUTCONN:L13,2,* FIDDLERSCV:I7,11,3,3
GOLDENISLESPKWY:J10,7,1,1 HILLPL:K10,103,11,902 KNOXDR:I10,5,1,1 MCKENZIEDR:H8,11,3,3 OLD99RD:H6,8,3,3
PLANTINGHAMMOCKBLVD:M7,9,6* SAVANNAHEVERETTAV:D5,8,3,* STUDANIELDR:I8,11,3,3 VOGELRD: ,5,1,5


__END__

I love Perl; it's the only language where you can bless your thingy.

Perl documentation is available at perldoc.perl.org. The list of standard modules and pragmatics is available in perlmodlib.

Get Markup Help. Please note the markup tag of "code".

 
 


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

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