Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

hello monks!!! i am a beginner to perl.plz help me out to solve this. how can i insert a newline(\n) character at 10th character position of all the lines in a text file. thanks a ton!!!
  • Comment on how to Insert a newline character in a sentence

Replies are listed 'Best First'.
Re: how to Insert a newline character in a sentence
by mzedeler (Pilgrim) on Jul 23, 2009 at 12:07 UTC

    Welcome to the perl community! Try looking at substr in perlfunc.

    Update: you may also get something out of the -p and -i switches. See more in perlrun.

      Try this
      while (<DATA>) { $var=$_; $var=~s/(.{10})/$1\\n/; print "$var"; } __DATA__ dgbjhdghjdfgjhd dfgjdjfghjkdhgdfg dgbhgdfjgjhgdfgdfg ghgjgglhkdjghkdhgk

        update: Or, to insert newlines at the 10th position and avoid double spacing lines of 9 characters, perhaps this

        use strict; use warnings; while (<DATA>) { my $var=$_; $var=~s/(.{9})/$1\n/; print "$var"; } __DATA__ dgbjhdghjdfgjhd dfgjdjfghjkdhgdfg dgbhgdfjgjhgdfgdfg ghgjgglhkdjghkdhgk
Re: how to Insert a newline character in a sentence
by apl (Monsignor) on Jul 23, 2009 at 12:24 UTC
    Why don't you show us what you've tried so far? (BTW, you realize that the 10th character of each line is not the end of a sentence... Do you want to rephrase the question, or the title of the post?)
Re: how to Insert a newline character in a sentence
by ikegami (Patriarch) on Jul 23, 2009 at 15:01 UTC
Re: how to Insert a newline character in a sentence
by rovf (Priest) on Jul 23, 2009 at 14:33 UTC

    How are lines with less than 10 characters to be handled?

    -- 
    Ronald Fischer <ynnor@mm.st>