How to insert a newline in Perl 
Author Message
 How to insert a newline in Perl



Quote:
> Hi all,
> I've tried using tr & sed to do the following but I have been unsuccessful.

> My Input:
> P0127aaaaaaaaaaa00P0203aaaaaaaaaaa08F0102aaaaaaaaaaaaaa00

> Desired Output:
> P0127aaaaaaaaaaa00
> P0203aaaaaaaaaaa08
> F0102aaaaaaaaaaaaaa00

> I cannot use 'fold' to do this as the length of each line is different.  How
> can I do this in Perl?

If you want the newline before every capital letter followed by four
digits (as it seems to me looking at your example) then try:

     s/([A-Z]\d{4})/\n$1/g;

Cheers,
Bernard



Sun, 16 May 2004 12:09:44 GMT  
 How to insert a newline in Perl
I've already tried your suggestion beforehand (sorry, forgot to mention what
I have tried) but it gave me no output.  As I mentioned before, the data is
just one long line of data with control characters in it as well.  If I try
to vi the file, I get a message at the bottom of the screen saying "file too
long".  If I run a 'file' on the file to check its file type, it just says
'data'.  Any other ideas?


Quote:


> > Hi all,
> > I've tried using tr & sed to do the following but I have been
unsuccessful.

> > My Input:
> > P0127aaaaaaaaaaa00P0203aaaaaaaaaaa08F0102aaaaaaaaaaaaaa00

> > Desired Output:
> > P0127aaaaaaaaaaa00
> > P0203aaaaaaaaaaa08
> > F0102aaaaaaaaaaaaaa00

> > I cannot use 'fold' to do this as the length of each line is different.
How
> > can I do this in Perl?

> If you want the newline before every capital letter followed by four
> digits (as it seems to me looking at your example) then try:

>      s/([A-Z]\d{4})/\n$1/g;

> Cheers,
> Bernard



Mon, 17 May 2004 10:17:52 GMT  
 How to insert a newline in Perl

[I moved your reply below the quoted material.  Please don't top-post.]

Quote:




> > > Hi all,
> > > I've tried using tr & sed to do the following but I have been
> unsuccessful.

> > > My Input:
> > > P0127aaaaaaaaaaa00P0203aaaaaaaaaaa08F0102aaaaaaaaaaaaaa00

> > > Desired Output:
> > > P0127aaaaaaaaaaa00
> > > P0203aaaaaaaaaaa08
> > > F0102aaaaaaaaaaaaaa00

> > > I cannot use 'fold' to do this as the length of each line is different.
> How
> > > can I do this in Perl?

> > If you want the newline before every capital letter followed by four
> > digits (as it seems to me looking at your example) then try:

> >      s/([A-Z]\d{4})/\n$1/g;

> > Cheers,
> > Bernard
> I've already tried your suggestion beforehand (sorry, forgot to mention what
> I have tried) but it gave me no output.  As I mentioned before, the data is
> just one long line of data with control characters in it as well.  If I try
> to vi the file, I get a message at the bottom of the screen saying "file too
> long".  If I run a 'file' on the file to check its file type, it just says
> 'data'.  Any other ideas?

If your data is what you say it is, Bernard's solution is just fine.
Just try (typed on one line):

    perl -le '$_ = "P0127aaaaaaaaaaa00P0203aaaaaaaaaaa08F0102aaaaaaaaaaaaaa00";
    s/([A-Z]\d{4})/\n$1/g; print'

What do you mean by "it gave me no output"?  The substitution by itself
doesn't give output, of course.  Did you print the string after the
substitution?

Anno



Mon, 17 May 2004 10:52:38 GMT  
 How to insert a newline in Perl

Quote:



> > Hi all,
> > I've tried using tr & sed to do the following but I have been unsuccessful.

> > My Input:
> > P0127aaaaaaaaaaa00P0203aaaaaaaaaaa08F0102aaaaaaaaaaaaaa00

> > Desired Output:
> > P0127aaaaaaaaaaa00
> > P0203aaaaaaaaaaa08
> > F0102aaaaaaaaaaaaaa00

> > I cannot use 'fold' to do this as the length of each line is different.  How
> > can I do this in Perl?

> If you want the newline before every capital letter followed by four
> digits (as it seems to me looking at your example) then try:

>      s/([A-Z]\d{4})/\n$1/g;

> Cheers,
> Bernard

   open( TEST, "test.dat") || die "Where dat dat at?: $!";

   $raw = <TEST>;



        print "$_\n";
   }

   close TEST;



Mon, 17 May 2004 17:26:48 GMT  
 How to insert a newline in Perl

Quote:



>> > Hi all,
>> > I've tried using tr & sed to do the following but I have been unsuccessful.

>> > My Input:
>> > P0127aaaaaaaaaaa00P0203aaaaaaaaaaa08F0102aaaaaaaaaaaaaa00

>> > Desired Output:
>> > P0127aaaaaaaaaaa00
>> > P0203aaaaaaaaaaa08
>> > F0102aaaaaaaaaaaaaa00

>> > I cannot use 'fold' to do this as the length of each line is different.  How
>> > can I do this in Perl?

>> If you want the newline before every capital letter followed by four
>> digits (as it seems to me looking at your example) then try:

>>      s/([A-Z]\d{4})/\n$1/g;

[snipped signature]

Quote:
>    open( TEST, "test.dat") || die "Where dat dat at?: $!";

>    $raw = <TEST>;



>         print "$_\n";
>    }

>    close TEST;

Look at the OP's desired output above and then look at what
your code outputs. See any difference?

Cheers,
Bernard



Tue, 18 May 2004 07:40:00 GMT  
 How to insert a newline in Perl


Quote:
> Anno, what type of a moron do you think I am?  Of course, I performed the
> substitution on the input line.

I don't recall Anno suggesting you are a moron, I only recall
him making a helpful suggestion. We all overlook even the simplest
things sometimes and Anno was making sure it didn't happen this
time. What the frack is wrong with that?

Cheers,
Bernard



Tue, 18 May 2004 10:49:42 GMT  
 How to insert a newline in Perl

Quote:
> Anno, what type of a moron do you think I am?  Of course, I performed the
> substitution on the input line.

I had no idea what kind of moron you are, that's why I asked.  I do now.

Anno



Tue, 18 May 2004 11:29:31 GMT  
 How to insert a newline in Perl

Quote:





> >> > Hi all,
> >> > I've tried using tr & sed to do the following but I have been unsuccessful.

> >> > My Input:
> >> > P0127aaaaaaaaaaa00P0203aaaaaaaaaaa08F0102aaaaaaaaaaaaaa00

> >> > Desired Output:
> >> > P0127aaaaaaaaaaa00
> >> > P0203aaaaaaaaaaa08
> >> > F0102aaaaaaaaaaaaaa00

> >> > I cannot use 'fold' to do this as the length of each line is different.  How
> >> > can I do this in Perl?

> >> If you want the newline before every capital letter followed by four
> >> digits (as it seems to me looking at your example) then try:

> >>      s/([A-Z]\d{4})/\n$1/g;

> [snipped signature]

> >    open( TEST, "test.dat") || die "Where dat dat at?: $!";

> >    $raw = <TEST>;



> >         print "$_\n";
> >    }

> >    close TEST;

> Look at the OP's desired output above and then look at what
> your code outputs. See any difference?

> Cheers,
> Bernard

oops. Good thing Im not a proffesional programmer. Sorry gixxer.


Tue, 18 May 2004 16:41:37 GMT  
 How to insert a newline in Perl
Anno, I guess it takes one to know one.


Wed, 19 May 2004 04:17:03 GMT  
 How to insert a newline in Perl

Quote:

>I've tried using tr & sed to do the following but I have been unsuccessful.

>My Input:
>P0127aaaaaaaaaaa00P0203aaaaaaaaaaa08F0102aaaaaaaaaaaaaa00

>Desired Output:
>P0127aaaaaaaaaaa00
>P0203aaaaaaaaaaa08
>F0102aaaaaaaaaaaaaa00

>I cannot use 'fold' to do this as the length of each line is different.  How
>can I do this in Perl?

        s/(?<=a\d\d)/\n/g;

assuming that you want to fold after the string of a's (at least one)
and 2 digits.

--
        Bart.



Wed, 19 May 2004 04:31:24 GMT  
 
 [ 10 post ] 

 Relevant Pages 

1. insert newlines in a long string

2. How to insert newline break into a long string

3. Can't insert newline

4. Insert newline into long string

5. Replacing multiple occurences of newline chars with a single newline char

6. mysql INSERT is broken by some charactors in the inserted record

7. How does Perl handle DOS newlines?

8. Perl Tutorial Book (was Re: SED GURUS: Some Newline Questions)

9. Perl source with DOS newlines on unix

10. Lack of newlines in browser output from perl program

11. Inserting a 'newline' in arrays?

12. SRC: pmdirs - print the perl module path, newline separated

 

 
Powered by phpBB® Forum Software