On 1/19/2010 6:03 PM, Perl Noob wrote:
I am AMAZED at the help available in this forum. It is an awesome
resource. I can see, though, that my situation needs to be stated
more clearly.
The data is not consistent throughout the entire file. I WISH I only
had to skip every other line. The problem is not quite that simple.
The data I need is always consistent within the file, but is not so
neat as to be on every other line. The common characteristic of the
data I need is that the record has an end of line marker followed by
65 spaces on the following line. Here is a better sample of what I
described:
_______BEGIN SAMPLE DATA FILE_________________
RandomJunkNothingImportantMoreJunk
StuffthatdoesntmatterWhocaresaboutthis
RECORD1FIELD1(3 spaces)RECORD1FIELD2(3 spaces)RECORD1FIELD3(newline)
(65 spaces)RECORD1FIELD4(12 spaces)RECORD1FIELD5
RECORD2FIELD1(3 spaces)RECORD2FIELD2(3 spaces)RECORD2FIELD3(newline)
(65 spaces)RECORD2FIELD4(12 spaces)RECORD2FIELD5
RandomJunkNothingImportantMoreJunk
StuffthatdoesntmatterWhocaresaboutthis
MoreJunkThatDoesntmatterStuffIdontwantWhocaresaboutthis
RECORD3FIELD1(3 spaces)RECORD3FIELD2(3 spaces)RECORD3FIELD3(newline)
(65 spaces)RECORD3FIELD4(12 spaces)RECORD3FIELD5
RECORD4FIELD1(3 spaces)RECORD4FIELD2(3 spaces)RECORD4FIELD3(newline)
(65 spaces)RECORD4FIELD4(12 spaces)RECORD4FIELD5
RECORD5FIELD1(3 spaces)RECORD5FIELD2(3 spaces)RECORD5FIELD3(newline)
(65 spaces)RECORD5FIELD4(12 spaces)RECORD5FIELD5
RECORD6FIELD1(3 spaces)RECORD6FIELD2(3 spaces)RECORD6FIELD3(newline)
(65 spaces)RECORD6FIELD4(12 spaces)RECORD6FIELD5
___________END SAMPLE DATA FILE ____________________
You will notice in the sample above that the only consistent items
between the usable data is the (newline) followed by (65 spaces).
Therefore if I could find a way to do a search and replace
s/(newline)(65spaces)//gi;
that would be great. I just need to get each (newline)followed by
(65spaces) and delete it. I just am not sure how to do that. My
brain hurts.
I think you're mentally fixed on a global regex solution. This
would require that you first read the entire file into memory.
You could do that, but I wouldn't. Below is but one alternative
(obviously "(65 spaces)" is just a placeholder):
1 #!/usr/bin/perl
2
3 use warnings;
4 use strict;
5
6 my $prev;
7 while( <DATA> ) {
8 unless( /^\(65 spaces\)/ ) {
9 $prev = $_;
10 next;
11 }
12 $_ = "$prev$_";
13 my @fields = split;
14 print "@fields\n";
15 }
16
17
18 __END__
19 RandomJunkNothingImportantMoreJunk
20 StuffthatdoesntmatterWhocaresaboutthis
21 RECORD1FIELD1 RECORD1FIELD2 RECORD1FIELD3
22 (65 spaces)RECORD1FIELD4 RECORD1FIELD5
23 RECORD2FIELD1 RECORD2FIELD2 RECORD2FIELD3
24 (65 spaces)RECORD2FIELD4 RECORD2FIELD5
25 RandomJunkNothingImportantMoreJunk
26 StuffthatdoesntmatterWhocaresaboutthis
27 MoreJunkThatDoesntmatterStuffIdontwantWhocaresaboutthis
28 RECORD3FIELD1 RECORD3FIELD2 RECORD3FIELD3
29 (65 spaces)RECORD3FIELD4 RECORD3FIELD5
30 RECORD4FIELD1 RECORD4FIELD2 RECORD4FIELD3
31 (65 spaces)RECORD4FIELD4 RECORD4FIELD5
32 RECORD5FIELD1 RECORD5FIELD2 RECORD5FIELD
33 (65 spaces)RECORD5FIELD4 RECORD5FIELD5
34 RECORD6FIELD1 RECORD6FIELD2 RECORD6FIELD3
35 (65 spaces)RECORD6FIELD4 RECORD6FIELD5
--
Brad
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/