how to edit a batch file

K

king

Suppose a batch file contains something like (some 250 lines like this)


REM //// TEST PCIE LINK ////
Win32 pd k0018f00 > test.log
fc /l good1.log test.log > comp.log
find "no differences encountered" comp.log
if errorlevel 1 goto end
echo Passed XV_UP test.
del test.log
del comp.log

I have written a perl script to edit some of the lines.
My script is
open my $F, '<', $file || die "Cant open $file: $!";
flock($F, 1) || die "Cant get LOCK_SH on $file: $!";
while(<$F>)
{
$line_counter ++;
if ($line_counter == 15)
{

&SUB_Link;
}
sub SUB_Link
{

if($_=~/k......./i)
{
s/k......./$any variable;
}
}
close $F || die "Cant close $file: $!";
}
but htis is not working
can anybody suggest something.
 
J

Josef Moellers

king said:
Suppose a batch file contains something like (some 250 lines like this)


REM //// TEST PCIE LINK ////
Win32 pd k0018f00 > test.log
fc /l good1.log test.log > comp.log
find "no differences encountered" comp.log
if errorlevel 1 goto end
echo Passed XV_UP test.
del test.log
del comp.log

I have written a perl script to edit some of the lines.
My script is
open my $F, '<', $file || die "Cant open $file: $!";
flock($F, 1) || die "Cant get LOCK_SH on $file: $!";
while(<$F>)
{
$line_counter ++;
if ($line_counter == 15)
{

&SUB_Link;
}
sub SUB_Link
{

if($_=~/k......./i)
{
s/k......./$any variable;
}
}
close $F || die "Cant close $file: $!";
}
but htis is not working
can anybody suggest something.

Correct the mistake!
 
G

Gunnar Hjalmarsson

king said:
open my $F, '<', $file || die "Cant open $file: $!";
flock($F, 1) || die "Cant get LOCK_SH on $file: $!";
while(<$F>)
{
$line_counter ++;
if ($line_counter == 15)
{

&SUB_Link;
}
sub SUB_Link
{

if($_=~/k......./i)
{
s/k......./$any variable;
}
}
close $F || die "Cant close $file: $!";
}
but htis is not working
can anybody suggest something.

The first suggestion that comes to mind is that you should have

use strict;
use warnings;

in the beginning of the script, to make Perl help you detect some kind
of mistakes.

Second: Don't post a script here that does not compile, and just say
that it's "not working". At least try to fix the compilation error(s),
and if you don't know how, explain what error message(s) you get.

Third: Check the Perl FAQ before asking for help. Your specific problem
is covered by this FAQ entry:

perldoc -q "change one line"

Fourth: There are posting guidelines available for this Usenet group,
that you also should study and follow:
http://www.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
 
J

Jürgen Exner

king said:
Suppose a batch file contains something like (some 250 lines like
this)


REM //// TEST PCIE LINK ////
Win32 pd k0018f00 > test.log
fc /l good1.log test.log > comp.log
find "no differences encountered" comp.log
if errorlevel 1 goto end
echo Passed XV_UP test.
del test.log
del comp.log

I have written a perl script to edit some of the lines.
My script is
open my $F, '<', $file || die "Cant open $file: $!";

Right idea, but wrong operator. || has a higher precedence than ,. Therefore
this line will be evaluated as
open my $F, '<', ($file || die "Cant open $file: $!");
I doubt that is what you want. Use "or" instead.
flock($F, 1) || die "Cant get LOCK_SH on $file: $!";
while(<$F>)
{
$line_counter ++;

What is this $line_counter thingy? Did you mean $. instead?
$. The current input record number for the last file handle [...]
if ($line_counter == 15)
{

&SUB_Link;

Is there a specific reason why you are calling the sub this way? Unless you
know why you are using the &-style you don't want to use it.
}
sub SUB_Link
{

if($_=~/k......./i)
{
s/k......./$any variable;

You are missing the closing /. This code should not even have passed the
syntax analyser.
And yes, based on the code fragments that you showed this should replace the
text.
}
}
close $F || die "Cant close $file: $!";
}
but htis is not working
can anybody suggest something.

Did you read "perldoc -q change":
How do I change one line in a file/delete a line in a file/insert a
line in the middle of a file/append to the beginning of a file?


jue
 
T

Tad McClellan

king said:
open my $F, '<', $file || die "Cant open $file: $!";


See the precedence table in perlop.pod.

open my $F, '<', $file or die "Cant open $file: $!";

or

open(my $F, '<', $file) || die "Cant open $file: $!";

$line_counter ++;


Perl already maintains a line counter for you. Its name is $. (dollar-dot).

&SUB_Link;


SUB_Link();

You should not use ampersands on subroutine calls unless you know why
you need to use ampersands on subroutine calls.

if($_=~/k......./i)
{
s/k......./$any variable;


The s/// operator needs _3_ slashes, you have only 2.

There is no need to m// before s/// since s/// will not do
anything if the match part fails.

close $F || die "Cant close $file: $!";


Precedence again.

can anybody suggest something.


Read (and follow) the Posting Guidelines that are posted here frequently.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top