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

Hi,

in a csv file how can i insert a character/special character on particular nth position in every row

for example file1.csv

Here is the sample data

2016-02-06-0240:00 2016-02-06-0540:00 2016-02-06-0740:00

expected output

2016-02-06-02:40:00 2016-02-06-05:40:00 2016-02-06-07:40:00

Replies are listed 'Best First'.
Re: in file insert character in every row on nth position
by hippo (Archbishop) on Feb 11, 2016 at 19:18 UTC
    perl -pi -e 's/^(.{13})/$1:/;' myfile

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

      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.

      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

Re: in file insert character in every row on nth position
by AnomalousMonk (Archbishop) on Feb 11, 2016 at 18:54 UTC

    It might be done with something like substr EXPR,OFFSET,LENGTH,REPLACEMENT:

    c:\@Work\Perl\monks>perl -wMstrict -le "my $s = 'abcdefg'; print qq{'$s'}; ;; my $char = 'X'; my $n = 3; substr $s, $n, 0, $char; print qq{'$s'}; " 'abcdefg' 'abcXdefg'

    Update: Changed example code to make position of insertion a variable.


    Give a man a fish:  <%-{-{-{-<

Re: in file insert character in every row on nth position
by NetWallah (Canon) on Feb 11, 2016 at 18:55 UTC
    Your specification does not match your example, for the second line.

    If followed, your line would be:

    cc-20,dd:-kk:jj,01,cc
    Would you care to revise/clarify the specification, or sample data ?

    UPDATE:The OP updated the original node without explanation/annotation. That removes the context for this node.

            "Think of how stupid the average person is, and realize half of them are stupider than that." - George Carlin

      YES,

      Here is the sample data

      2016-02-06-0240:00 2016-02-06-0540:00 2016-02-06-0740:00

      expected output

      2016-02-06-02:40:00 2016-02-06-05:40:00 2016-02-06-07:40:00