> On Tue, Jan 19, 2010 at 08:12:25AM -0500, Perl Noob wrote:
>> > å¨ 2010-01-19äºç 00:09 -0500ï¼Perl Noobåéï¼
>> >> 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:
>> >>
>> >
>> > HI,
>> >
>> > If you are using a regex, then may want to try the /m option.
>> > see perldoc perlre for details.
>> > I give the code below, it could work for me.
>> >
>> >
>> > use strict;
>> >
>> > local $/="RECORD1FIELD5\n";
>> >
>> > while(<DATA>) {
>> > my @fields = /\w+/gm;
>> > print "@fields\n";
>> > }
>> >
>> >
>> > __DATA__
>> > RECORD1FIELD1 RECORD1FIELD2 RECORD1FIELD3 RECORD1FIELD3
>> > RECORD1FIELD4 RECORD1FIELD5
>> >
>> > RECORD2FIELD1 RECORD2FIELD2 RECORD2FIELD3 RECORD2FIELD3
>> > RECORD2FIELD4 RECORD2FIELD5
>> >
>> >
>>
>> Your example works if RECORD1FIELD5 is a constant value and is the
>> same as RECORD2FIELD5, it is not. RECORD1FIELD5 is different from
>> RECORD2FIELD5 which will be different from RECORD3FIELD5. There is
>> the problem. I need to find a way to delete the \n at the end of
>> the
>> first line of the record, but maintain the \n on the second line of
>> the record.
>>
>> That way each record will be on a single line instead of spanning
>> two
>> lines.
>
> Something like this will probably do what you want:
>
> $ perl -00pe 's/\n/ / < input > output
>
> or you may prefer:
>
> $ perl -00pe 's/\s+/ /g; s/ $/\n/' < input > output
>
> The key here is -00 which sets the IRS to paragraph mode ($/ = "")-
> See
> perldoc perlrun and perldoc perlvar.
>
> --
> Paul Johnson - [email protected]
> http://www.pjcj.net
I really don't know the -00 very well, but my understanding is that it
expects a blank line after the record. My file does not have a blank
line, all of the lines are consecutive, but each record consists of
two lines.
I am going to try this suggestion and will let you know.
Thank you very much for the input and guidance.
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/