mlm has asked for the wisdom of the Perl Monks concerning the following question:
I was asked to solve a problem at work so I tried to make it as short possible.
Problem:
Read and Modify a file in place containing lines in the format of "variable = number" taking into consideration any whitespace around the equal sign.
Example input:
one=1 two=2 three=3 xyz=20
Increment the value of a specific variable in the file and write it back out.
Ex: where variable is xyz.
one=1 two=2 three=3 xyz=21
My attempt:
where xyz is the variable whose value is being incremented by 1 and test.data is the file containing data.
-mlmperl -p -i.bak -e 's/$1/$a/ if ((/xyz\s*=\s*([0-9]*)\s*/)&&($a=$1+1))' + test.data
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Can anyone make this shorter?
by CheeseLord (Deacon) on Aug 21, 2001 at 19:45 UTC | |
by George_Sherston (Vicar) on Aug 21, 2001 at 20:37 UTC | |
|
Re: Can anyone make this shorter?
by maverick (Curate) on Aug 21, 2001 at 19:50 UTC | |
|
Re: Can anyone make this shorter?
by japhy (Canon) on Aug 21, 2001 at 22:08 UTC | |
by tilly (Archbishop) on Aug 22, 2001 at 06:05 UTC | |
|
Re: Can anyone make this shorter?
by kschwab (Vicar) on Aug 21, 2001 at 19:56 UTC | |
|
Re: Can anyone make this shorter?
by MZSanford (Curate) on Aug 21, 2001 at 19:52 UTC | |
by mlm (Novice) on Aug 21, 2001 at 20:14 UTC |