@reports = `ls *.doc`; foreach $file(@reports) { $cmd=`perl -ni -e 'print;print "VALUE=KEY\n" if \$\.== 3;' $file` }

The above code is working fine now, with this i can able to add a line to around 1400 report files.

That script will be creating ~1400 perl processes. You've been repeatedly advised about this yet you continue to do it.

This script performs exactly the same function but only uses one perl process:

#!/usr/bin/env perl -i use strict; use warnings; while (<>) { print; print "VALUE=KEY\n" if $. == 3; $. = 0 if eof; }

I suggest you read "perlrun - how to execute the Perl interpreter". Amongst other things, this will tell you about the Command Switches: perhaps lack of knowledge in this area is why you're coding your script the way you are.

I put the above code in pm_1076103.pl and created three *.doc files:

$ ls -al *.doc -rw-r--r-- 1 ken staff 52 26 Feb 03:20 pm_1076103_1.doc -rw-r--r-- 1 ken staff 52 26 Feb 03:22 pm_1076103_2.doc -rw-r--r-- 1 ken staff 52 26 Feb 03:22 pm_1076103_3.doc $ cat pm_1076103_1.doc Doc 1 Line 1 Doc 1 Line 2 Doc 1 Line 3 Doc 1 Line 4 $ cat pm_1076103_2.doc Doc 2 Line 1 Doc 2 Line 2 Doc 2 Line 3 Doc 2 Line 4 $ cat pm_1076103_3.doc Doc 3 Line 1 Doc 3 Line 2 Doc 3 Line 3 Doc 3 Line 4

I then ran the script like this:

$ pm_1076103.pl *.doc

Here's what the *.doc files now look like:

$ cat pm_1076103_1.doc Doc 1 Line 1 Doc 1 Line 2 Doc 1 Line 3 VALUE=KEY Doc 1 Line 4 $ cat pm_1076103_2.doc Doc 2 Line 1 Doc 2 Line 2 Doc 2 Line 3 VALUE=KEY Doc 2 Line 4 $ cat pm_1076103_3.doc Doc 3 Line 1 Doc 3 Line 2 Doc 3 Line 3 VALUE=KEY Doc 3 Line 4

-- Ken


In reply to Re^3: Problem in executing the perl oneliner within the script (calling perl from perl) by kcott
in thread Problem in executing the perl oneliner within the script by perladdict

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.