Increment Value In Substitution

Discussion in 'Perl Misc' started by Hike Mike, Aug 4, 2005.

  1. Hike Mike

    Hike Mike Guest

    I want to use perl to add 2 lines of code to every .conf file in all
    subdirectories of a given path:

    D:\projects\edge\workspace>echo find -path "*"config"*".conf | xargs
    perl -pi.ba
    k -e
    "s/^DB_CONNECT=true/JMX_HTTP_ADDR=127.0.0.1\nJMX_HTTP_PORT=8090\n\nDB_CONNE
    CT=true/"

    The problem is that the JMX_HTTP_PORT will be the same in all config
    files. It would save me a lot of time if I could increment the port
    number after every substitution...something tells me it's time to
    script this.
    Hike Mike, Aug 4, 2005
    #1
    1. Advertising

  2. Hike Mike wrote:
    > I want to use perl to add 2 lines of code to every .conf file in all
    > subdirectories of a given path:
    >
    > D:\projects\edge\workspace>echo find -path "*"config"*".conf | xargs
    > perl -pi.ba
    > k -e
    >

    "s/^DB_CONNECT=true/JMX_HTTP_ADDR=127.0.0.1\nJMX_HTTP_PORT=8090\n\nDB_CONNE
    > CT=true/"
    >
    > The problem is that the JMX_HTTP_PORT will be the same in all config
    > files.


    If it's the same in each file, where's the problem?


    > It would save me a lot of time if I could increment the port
    > number after every substitution...something tells me it's time to
    > script this.
    >


    perldoc File::Find

    perldoc Tie::File

    jimk
    James E Keenan, Aug 5, 2005
    #2
    1. Advertising

  3. Hike Mike

    Guest

    Hike Mike wrote:

    [ simplified ]

    perl -pe "s/^DB_CONNECT=true/JMX_HTTP_PORT=8090/"

    > It would save me a lot of time if I could increment the port
    > number after every substitution...


    perl -pe "BEGIN{$p=8090}s/^DB_CONNECT=true/JMX_HTTP_PORT=@{[$p++]}/"

    > something tells me it's time to script this.


    Yes, probably.
    , Aug 5, 2005
    #3
  4. Hike Mike

    Guest

    wrote:
    >
    > perl -pe "BEGIN{$p=8090}s/^DB_CONNECT=true/JMX_HTTP_PORT=@{[$p++]}/"


    Just reliased the OP had 'perl -p' driven by xargs. If the number of
    arguments is higher than xargs is happy to put on one command line then
    $p will reset.
    , Aug 5, 2005
    #4
  5. Hike Mike

    Guest

    wrote:
    > wrote:
    > >
    > > perl -pe "BEGIN{$p=8090}s/^DB_CONNECT=true/JMX_HTTP_PORT=@{[$p++]}/"

    >
    > Just reliased the OP had 'perl -p' driven by xargs. If the number of
    > arguments is higher than xargs is happy to put on one command line then
    > $p will reset.



    A persistent data structure might do, though it makes
    for a long and ugly one-liner:

    xargs --max-args=1 perl -MStorable=nstore,retrieve -lpe 'BEGIN { $sref
    = -f "file" ? retrieve "file" :
    \8090};s/^DB_CONNECT=true/JMX_HTTP_PORT=$$sref/; END { nstore \($$sref
    + 1), "file"};'

    --
    Regards,
    Steven
    , Aug 5, 2005
    #5
  6. Hike Mike

    Hike Mike Guest

    yep,
    I scripted it after taking heed of perldoc Tie::File

    __BEGIN__

    #!/usr/bin/perl
    #Starkie, Michael C.

    ##############################################################################
    # THIS SCRIPT WILL DO A UNIX LIKE FIND ON ALL FILES IN A DIRECTORY
    INCLUDING #
    # SUB-DIRECTORIES AND SEARCH FOR FILES THAT END WITH THE EXTENSION
    SUPPLIED #
    # TO THE SCRIPT. WHEN A FILE IS FOUND IT WILL IT WILL REPLACE A VALUE
    INT #
    # IN THE STRING WITH AN INCREMENTED VERSION OF THE VALUE. RIGHT NOW
    THESE #
    # VALUES ARE ALL HARDCODED BUT EASILY REPLACEABLE.
    #
    #
    #
    # USAGE: incValue D:\mypath .conf
    #
    ##############################################################################

    use strict;
    use warnings;

    use File::Find;
    use Tie::File;
    use File::Basename;

    die <<ERR unless @ARGV==2;
    Supply a source path for the files you want to modify and a file
    extension of
    the type of file you want to search for.
    ERR

    my $fromPath = $ARGV[0];
    my $ext = $ARGV[1];

    fileparse_set_fstype("MSDOS");

    ###########################################################
    # DO A UNIX-LIKE FIND ON ALL THE FILES IN THE SOURCE PATH #
    ###########################################################

    my $port = 8090;

    find (\&search, $fromPath);

    sub search {
    return unless grep { /$ext$/ } $_;
    tie my @array, 'Tie::File', $_ or die "CAN'T OPEN FILE: $_\n";
    for (@array) {
    s/^JMX_HTTP_PORT=8090/JMX_HTTP_PORT=$port/;
    }

    $port++;

    }

    __END__
    Hike Mike, Aug 7, 2005
    #6
    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. Replies:
    104
    Views:
    10,992
    Jordan Abel
    Oct 28, 2005
  2. Replies:
    99
    Views:
    2,491
    eliza81
    Jun 11, 2010
  3. Robben
    Replies:
    14
    Views:
    560
    Old Wolf
    Dec 27, 2005
  4. Alf P. Steinbach /Usenet
    Replies:
    0
    Views:
    889
    Alf P. Steinbach /Usenet
    May 22, 2011
  5. Peng Yu

    post increment or pre increment?

    Peng Yu, Nov 21, 2008, in forum: Perl Misc
    Replies:
    7
    Views:
    518
    Peter J. Holzer
    Nov 23, 2008
Loading...

Share This Page