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

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: insert line in between two lines
by holli (Abbot) on Mar 16, 2005 at 11:39 UTC
    perl -ni -e "print qq($_\n);" yourfile.name


    holli, /regexed monk/
Re: insert line in between two lines
by prasadbabu (Prior) on Mar 16, 2005 at 11:32 UTC
    s/\n([^\n]*)/\n$newline$1/s;

    update:'\n' removed from match pattern.

    Prasad

      here is what to i needed specifically
      $cwd = cwd();
      print"$cwd\n";

      i want the following
      $cwd = cwd();
      $exec = $ENV{EXEC}
      print"$cwd\n";

      i am sorry for not giving detail earlier

        This might work for you.

        undef $/; $a = <DATA>; $nline =q($exec = $ENV{EXEC};); $a =~ s/\n([^\n]+)/\n$nline\n$1/s; print "$a"; __DATA__ $cwd = cwd(); print"$cwd\n";

        Prasad

Re: insert line in between two lines
by tphyahoo (Vicar) on Mar 16, 2005 at 11:50 UTC
    This sticks a newline between every line in the input file. You also get a blank line at the end, which strictly speaking, is not "between" two lines.
    #perl linesBetween.pl linesBetweenTest.txt #makes backup just in case use strict; use warnings; my $infile = shift; $infile =~ /(.*)(\.txt$)/ or die 'Input file must be of type .txt'; my $backupfile = "$1.bak"; my $tmpfile = "$1.tmp"; `copy $infile $1.bak`; my $F; my %lines; open F, "< $infile" or die "couldn't open $infile"; open G, "> $tmpfile" or die "couldn't open $tmpfile"; while (<F>) { print G "$_\n"; } close F; close G; `del $infile`; `move $tmpfile $infile`;
Re: insert line in between two lines
by perlsen (Chaplain) on Mar 16, 2005 at 11:58 UTC
    undef $/; open(IN, "< 1.txt") ; $str = <IN>; close(IN); $str =~ s#\n(.+?)\n#\n\$exec \= \$ENV\{EXEC\}\n$1\n#gsi; open(OUT, "> 2.txt") ; print OUT $str; input: ********* hai this is the test input sample output: ********** hai $exec = $ENV{EXEC} this is $exec = $ENV{EXEC} the test $exec = $ENV{EXEC} input sample
      perlsen thanks, but as i told earlier that i need to add
      $exec = $ENV{EXEC}
      only once in between
      $cwd=cwd();
      and
      print"$cwd\n"; i hope you will get what i want .

        Remove 'g' option modifier for that.

        Prasad

Re: insert line in between two lines
by chas (Priest) on Mar 16, 2005 at 13:56 UTC
    Assuming you know the line number after which you want to add the new line (say 2; in your example it's 1), you could do something like:
    my $linenum=2; my $linetoadd="Something\n"; my $i; while(<>){ ++$i; if($i==$linenum) {print;print $linetoadd;} else {print;} }
    (You can direct the output to a new file.)
    You could also arrange things so the line number and new line are given as args, depending on how this is to be used. (i.e a one time run or or a utility used frequently; of course, if it's just a one time thing, I'd simply use an editor.)
    Hope this is what you wanted.
    chas