Increment Value In Substitution

H

Hike Mike

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

James E Keenan

Hike said:
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
 
N

nobull

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

nobull

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

attn.steven.kuo

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"};'
 
H

Hike Mike

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__
 

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,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top