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
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/