On 1/19/2010 12:09 AM, Perl Noob wrote:
I have a data file with thousands of records.  The problem is that the
records in the data file span two lines for each record.  I want to
write a perl script that makes each record a single line.  The file
looks like this:

RECORD1FIELD1  RECORD1FIELD2     RECORD1FIELD3  RECORD1FIELD3
           RECORD1FIELD4          RECORD1FIELD5

RECORD2FIELD1  RECORD2FIELD2     RECORD2FIELD3  RECORD2FIELD3
           RECORD2FIELD4          RECORD2FIELD5

  . . .

What I want is this:

RECORD1FIELD1  . . .RECORD1FIELD5
RECORD2FIELD1  . . .RECORD2FIELD5


The second line of each record actually has a bunch of spaces before
the first field.  I thought I could exploit this with:

s/\n                                //gi;

what I thought would happen is the script would look for a new line
followed by a bunch of empty spaces and delete only those.  But that
didn't work.

Using a hex editor I saw that each new line was 0D 0A. I then tried:

s/\x0D\x0A//gi;

that didn't work either.

I just want to move the second line of each record to the end of the
first.  It seems so simple, but I am exhausted of trying different
things.





I see a couple of choices.  Your example data seems to have an
extra newline between logical records.  If that's true, then
you can read them as paragraphs, e.g.,

     1  #!/usr/bin/perl
     2
     3  use warnings;
     4  use strict;
     5
     6  $/ = "\n\n";  # one of the paragraph modes
     7
     8  while( <DATA> ) {
     9      my @fields = split;
    10      print "@fields\n";
    11  }
    12
    13
    14  __DATA__
    15  RECORD1FIELD1  RECORD1FIELD2     RECORD1FIELD3  RECORD1FIELD3
    16            RECORD1FIELD4          RECORD1FIELD5
    17
    18  RECORD2FIELD1  RECORD2FIELD2     RECORD2FIELD3  RECORD2FIELD3
    19            RECORD2FIELD4          RECORD2FIELD5
    20

If the apparent extra newline was not intentional, then
you could simply read two lines at a time, e.g.,

     1  #!/usr/bin/perl
     2
     3  use warnings;
     4  use strict;
     5
     6  while( <DATA> ) {
     7      $_ .= <DATA>;
     8      my @fields = split;
     9      print "@fields\n";
    10  }
    11
    12
    13  __DATA__
    14  RECORD1FIELD1  RECORD1FIELD2     RECORD1FIELD3  RECORD1FIELD3
    15            RECORD1FIELD4          RECORD1FIELD5
    16  RECORD2FIELD1  RECORD2FIELD2     RECORD2FIELD3  RECORD2FIELD3
    17            RECORD2FIELD4          RECORD2FIELD5


--
Brad

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to