Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to insert new lines in string?

0 views
Skip to first unread message

roach

unread,
Oct 21, 2004, 7:02:45 PM10/21/04
to
Hi
I have a file that is 1 line but 1000's of characters long.
How can I insert a newline every nth character? or would sed be better for
this?

Thanks


Tad McClellan

unread,
Oct 21, 2004, 7:46:49 PM10/21/04
to
roach <joe...@cfl.rr.com> wrote:

> How can I insert a newline every nth character?


$string =~ s/(.{$n})/$1\n/g;


--
Tad McClellan SGML consulting
ta...@augustmail.com Perl programming
Fort Worth, Texas

Jon Ericson

unread,
Oct 21, 2004, 8:05:01 PM10/21/04
to
"roach" <joe...@cfl.rr.com> writes:

fold or fmt would be better. There is a perl implementation of fold
as a part of the Perl Power Tools bundle, which is available on CPAN.
(search.cpan.org isn't responding at the moment or I'd give you a
direct link.)

Jon

Abigail

unread,
Oct 22, 2004, 3:16:30 AM10/22/04
to
roach (joe...@cfl.rr.com) wrote on MMMMLXIX September MCMXCIII in
<URL:news:peXdd.19$Mc...@tornado.tampabay.rr.com>:
`` Hi

`` I have a file that is 1 line but 1000's of characters long.
`` How can I insert a newline every nth character? or would sed be better for
`` this?

Untested:

perl -i -wle '$\ = \80; print "$_\n" while <>' your_file

Change 80 to 'n'.

Abigail
--
perl -MLWP::UserAgent -MHTML::TreeBuilder -MHTML::FormatText -wle'print +(
HTML::FormatText -> new -> format (HTML::TreeBuilder -> new -> parse (
LWP::UserAgent -> new -> request (HTTP::Request -> new ("GET",
"http://work.ucsd.edu:5141/cgi-bin/http_webster?isindex=perl")) -> content))
=~ /(.*\))[-\s]+Addition/s) [0]'

Anno Siegel

unread,
Oct 22, 2004, 4:46:05 AM10/22/04
to
roach <joe...@cfl.rr.com> wrote in comp.lang.perl.misc:

my $n = 30;
substr( $long, $n*$_, 0, "\n") for reverse 1 .. length( $long)/$n;

Anno

roach

unread,
Oct 22, 2004, 9:43:26 PM10/22/04
to

"Tad McClellan" <ta...@augustmail.com> wrote in message
news:slrncngij9...@magna.augustmail.com...


Thanks for the replys!

It works like a charm =)


roach

unread,
Oct 22, 2004, 9:44:56 PM10/22/04
to
Thanks!
I works like a charm, =)

"Tad McClellan" <ta...@augustmail.com> wrote in message
news:slrncngij9...@magna.augustmail.com...

Matt Garrish

unread,
Oct 22, 2004, 10:18:52 PM10/22/04
to

"roach" <joe...@cfl.rr.com> wrote in message
news:2Hied.14440$1f....@tornado.tampabay.rr.com...

>
> "Tad McClellan" <ta...@augustmail.com> wrote in message
> news:slrncngij9...@magna.augustmail.com...
>> roach <joe...@cfl.rr.com> wrote:
>>
>> > How can I insert a newline every nth character?
>>
>>
>> $string =~ s/(.{$n})/$1\n/g;
>>
>>

<snip signature>

>
>
> Thanks for the replys!
>
> It works like a charm =)
>

Weren't sure whether to top post or not? You got it right this time, but no
need to quote an entire message plus signature.

Matt


0 new messages