how to edit a batch file

Discussion in 'Perl Misc' started by king, Jul 14, 2006.

  1. king

    king Guest

    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.
     
    king, Jul 14, 2006
    #1
    1. Advertising

  2. king wrote:
    > 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!

    --
    Josef Möllers (Pinguinpfleger bei FSC)
    If failure had no penalty success would not be a prize
    -- T. Pratchett
     
    Josef Moellers, Jul 14, 2006
    #2
    1. Advertising

  3. king wrote:
    >
    > 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

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Jul 14, 2006
    #3
  4. king wrote:
    > 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
     
    Jürgen Exner, Jul 14, 2006
    #4
  5. king <> wrote:

    > 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.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Jul 14, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Luc The Perverse

    Compilation Batch File Won't Pipe To File

    Luc The Perverse, Oct 22, 2005, in forum: Java
    Replies:
    7
    Views:
    4,907
    Roedy Green
    Oct 23, 2005
  2. =?Utf-8?B?bWF0bHlu?=

    creating a file within a subdir in DOS batch file

    =?Utf-8?B?bWF0bHlu?=, Sep 20, 2007, in forum: ASP .Net
    Replies:
    0
    Views:
    658
    =?Utf-8?B?bWF0bHlu?=
    Sep 20, 2007
  3. Sriram
    Replies:
    1
    Views:
    1,042
  4. batch file/jar file

    , Feb 15, 2008, in forum: Java
    Replies:
    2
    Views:
    545
  5. abhaybhat
    Replies:
    2
    Views:
    323
    rahul
    Jun 11, 2008
Loading...

Share This Page