One-Liner Mania

L

Lars Eighner

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
 
M

Marc Girod

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
 
L

Lars Eighner

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

Marc Girod

You want the /e switch.

I can see that I obviously missed the question,
focusing as I did on making sure that the single
string in the file I changed would be the one
matching the file name, and not even noticing
the issue of incrementing the number.

So, thanks for the /e switch which I didn't know.
Thanks also for the explanation about the .bak
files on cygwin.

Now, if the files also happen to contain the
names of other files in the list, these other
lines would be modified as well.
We were not told what else the files contain...
In fact, I assumed that the first (or an early)
line would contain the file name, but that there
could be lots of data below, which is why I
attempted to close the file after the
replacement, which with -pi, wasn't a good idea.

a> cat Fife_Ch3.html
Fife_Ch0.html
Fife_Ch1.html
Fife_Ch2.html
Fife_Ch3.html
a> perl -pi -e 's/^Fife_Ch(\d+)\.html$/$1 + 1/e if /^${ARGV}$/'
Fife_Ch*.html
a> rm *.bak
a> ls -l
total 12
-rw-r--r--+ 1 marc None 44 2010-02-21 10:40 Fife_Ch0.html
-rw-r--r--+ 1 marc None 44 2010-02-21 10:40 Fife_Ch1.html
-rw-r--r--+ 1 marc None 44 2010-02-21 10:40 Fife_Ch2.html
-rw-r--r--+ 1 marc None 44 2010-02-21 10:40 Fife_Ch3.html
a> cat Fife_Ch3.html
Fife_Ch0.html
Fife_Ch1.html
Fife_Ch2.html
4

Marc
 

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,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top