in reply to Re^2: Problem in executing the perl oneliner within the script
in thread Problem in executing the perl oneliner within the script
@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
|
|---|