From: Lars Eighner on
Can I capture a group of digits, perform integer arithmetic on them,
and use them in the replacement?

Say I have a group of files named Fife_Ch01.html, Fife_Ch02.html
and so forth. The files all have a line with their own name. I want
to replace the file name with the next number and do not care about
leading zeros.

So,

Fife_Ch01.html is replaced with 2
Fife_Ch02.html is replaced with 3
and so forth.

Can I do this with a one-liner?

it would look something like this:

#perl -pi -e 's/Fife_Ch([0-9]*)\.html{a miracle happens}/\1/' Fife_Ch*.html




--
Lars Eighner <http://larseighner.com/> Warbama's Afghaninam day: 80
1941.0 hours since Warbama declared Viet Nam II.
Warbama: An LBJ for the Twenty-First century. No hope. No change.
From: Marc Girod on
On Feb 20, 10:10 pm, Lars Eighner <use...(a)larseighner.com> wrote:

> Can I do this with a one-liner?

perl -pi -e '($b)=$ARGV=~/^Fife_Ch(\d+)\.html$/;close ARGV if s/$ARGV/
$b/' Fife_Ch*.html

On my home box (cygwin), this leaves me with .bak files which I didn't
expect.

Marc
From: Ben Morrow on

Quoth Lars Eighner <usenet(a)larseighner.com>:
> Can I capture a group of digits, perform integer arithmetic on them,
> and use them in the replacement?

You want the /e switch.

> Say I have a group of files named Fife_Ch01.html, Fife_Ch02.html
> and so forth. The files all have a line with their own name. I want
> to replace the file name with the next number and do not care about
> leading zeros.
>
> So,
>
> Fife_Ch01.html is replaced with 2
> Fife_Ch02.html is replaced with 3
> and so forth.
>
> Can I do this with a one-liner?
>
> it would look something like this:
>
> #perl -pi -e 's/Fife_Ch([0-9]*)\.html{a miracle happens}/\1/' Fife_Ch*.html

Don't use \1 on the replacement side of s///. It was a sop to sed users
put into perl 1 that has never been removed.

perl -pi -e's/Fife_Ch(\d*)\.html/$1 + 1/e' Fife_Ch*.html

Ben

From: Ben Morrow on

Quoth Marc Girod <marc.girod(a)gmail.com>:
> On Feb 20, 10:10�pm, Lars Eighner <use...(a)larseighner.com> wrote:
>
> > Can I do this with a one-liner?
>
> perl -pi -e '($b)=$ARGV=~/^Fife_Ch(\d+)\.html$/;close ARGV if s/$ARGV/
> $b/' Fife_Ch*.html
>
> On my home box (cygwin), this leaves me with .bak files which I didn't
> expect.

Win32 can't use the Unix trick of deleting a file while it's open, so -i
always makes a backup. (Arguably perl should clean them up afterwards,
but it doesn't.)

Ben

From: Lars Eighner on
In our last episode, <269657-r571.ln1(a)osiris.mauzo.dyndns.org>, the lovely
and talented Ben Morrow broadcast on comp.lang.perl.misc:

> Quoth Lars Eighner <usenet(a)larseighner.com>:
>> Can I capture a group of digits, perform integer arithmetic on them,
>> and use them in the replacement?

> You want the /e switch.

Oh cool. This seems fraught with possibilities.

>> Say I have a group of files named Fife_Ch01.html, Fife_Ch02.html
>> and so forth. The files all have a line with their own name. I want
>> to replace the file name with the next number and do not care about
>> leading zeros.
>>
>> So,
>>
>> Fife_Ch01.html is replaced with 2
>> Fife_Ch02.html is replaced with 3
>> and so forth.
>>
>> Can I do this with a one-liner?
>>
>> it would look something like this:
>>
>> #perl -pi -e 's/Fife_Ch([0-9]*)\.html{a miracle happens}/\1/' Fife_Ch*.html

> Don't use \1 on the replacement side of s///. It was a sop to sed users

But ... but I *am* a sed user! I want my sop!

Actually I mostly use one-liners in shell scripts. I double-quote lots of
stuff and just hate escaping $s.

> put into perl 1 that has never been removed.

> perl -pi -e's/Fife_Ch(\d*)\.html/$1 + 1/e' Fife_Ch*.html

--
Lars Eighner <http://larseighner.com/> Warbama's Afghaninam day: 81
1949.4 hours since Warbama declared Viet Nam II.
Warbama: An LBJ for the Twenty-First century. No hope. No change.