in reply to in file insert character in every row on nth position

perl -pi -e 's/^(.{13})/$1:/;' myfile

See perlre and perlrun for documentation. Replace the 13 with other n as required.

Replies are listed 'Best First'.
Re^2: in file insert character in every row on nth position
by ukhare (Novice) on Feb 11, 2016 at 19:41 UTC

    your command works perfectly.any clue how to write this command in a script ?

      any clue how to write this command in a script ?

      Sure, that's an FAQ.

      Let Perl (well B::Deparse ) eplain it to you:
      perl -pi -e "s/^(.{13})/$1:/;" -MO=Deparse BEGIN { $^I = ""; } LINE: while (defined($_ = <ARGV>)) { s/^(.{13})/$1:/; } continue { die "-p destination: $!\n" unless print $_; } -e syntax OK

      L*

      There are no rules, there are no thumbs..
      Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
Re^2: in file insert character in every row on nth position
by ukhare (Novice) on Feb 11, 2016 at 19:25 UTC

    please tell me in format of script...all i am doing is inside a script. Unfortunately i am not much familiar about how to use commands inside perl script.

      "Unfortunately i am not much familiar about how to use commands inside perl script."

      This would be a good place to start: perlintro: Perl introduction for beginners

      This document will lead you through the creation of a Perl script. It's not particularly lengthy and shouldn't take you long to read. Each section has links to more detailed information: follow as necessary.

      — Ken

      I am pretty new and I was wondering about this too. I asked the question on the chatterbox and this is the result.

      [JobC] I occasionally read the questions and answers posted on SOPW +. Often I will see answers that show how to do something from a comma +nd line. How can this then be converted to a script? [shmem] essentially you put the quoted text after -e into a file [MidLifeXis] Which platform? [JobC] Like this one: in file insert character in every row on nth +position [JobC] i am using strawberry perl on windows [MidLifeXis] There may be some nuances based on Windows/Unix(sh,ksh +,csh,zsh)/VMS, and so on. [MidLifeXis] Is your environment set up to run .pl scripts on click +? [JobC] I have never tried that. I am using cmd shell [MidLifeXis] If you do SET PATHEXT, is .pl in the provided list? [shmem] then you add a line # *perl * at the top and add switches a +fter perl [MidLifeXis] perl -Mlib1 -Mlib2 -e 'foo(bar())' [JobC] nope .pl is not there.\ [shmem] shmem fades out [MidLifeXis] Then just do as shmem is saying, and execute: perl scr +iptname.pl [MidLifeXis] My example above would go into foo.pl as "use lib1;\nu +se lib2\nfoo(bar());\n" (replace '\n' with a newline) [JobC] Ok, Thanks! [MidLifeXis] Recommendation: always (or at least until you know why + you shouldn't) use use strict; use warnings at the top of your scrip +t. [MidLifeXis] Helps to catch very comment typos and mistakes. [MidLifeXis] *common, not comment

      hopefully this helps you as much as it helped me