You are not logged in.

#1 2009-09-25 19:24:57

anotherjohn
Member
Registered: 2009-09-23
Posts: 10

[SOLVED]What is wrong with this invocation of prename?

Hi

I've got a bunch of files with numbers in the name. I want to rename the ones numbered 1 - 9 to add a leading zero, so they sort correctly lexicographically.

I used $1 to back-reference the digit, but it didn't work and I don't know why:

[~] prename -n "s/_(\d)\.txt/_0$1\.txt/" *
file_1.txt renamed as file_0.txt
file_2.txt renamed as file_0.txt
file_3.txt renamed as file_0.txt
file_4.txt renamed as file_0.txt
file_5.txt renamed as file_0.txt
file_6.txt renamed as file_0.txt
file_7.txt renamed as file_0.txt
file_8.txt renamed as file_0.txt
file_9.txt renamed as file_0.txt

Any help appreciated.

Last edited by anotherjohn (2009-09-25 21:46:27)

Offline

#2 2009-09-25 20:07:46

brisbin33
Member
From: boston, ma
Registered: 2008-07-24
Posts: 1,799
Website

Re: [SOLVED]What is wrong with this invocation of prename?

uh oh! i hope you backed up before you ran that

i can't speak for prename, but in /this/ case, rename would've worked fine:

┌─[ 16:05 ][ blue:~ ]
└─> touch file_{1..9}.txt
┌─[ 16:05 ][ blue:~ ]
└─> ls file*
file_1.txt  file_4.txt  file_7.txt
file_2.txt  file_5.txt  file_8.txt
file_3.txt  file_6.txt  file_9.txt
┌─[ 16:05 ][ blue:~ ]
└─> rename file_ file_0 file*.txt
┌─[ 16:05 ][ blue:~ ]
└─> ls file*
file_01.txt  file_04.txt  file_07.txt
file_02.txt  file_05.txt  file_08.txt
file_03.txt  file_06.txt  file_09.txt

sorry for the hindsight advice smile.  hopefully a prename user will come along and tell you why what you did didn't work.

Offline

#3 2009-09-25 20:26:36

N-Acc
Member
Registered: 2009-02-19
Posts: 18

Re: [SOLVED]What is wrong with this invocation of prename?

try escaping the `$1' in your regex or use single quotes.. otherwise it is intepreted by the shell.

edit: I'm not sure how prename works .. but it looks weird to me that $1 is used to refer to a group instead of \1.

Last edited by N-Acc (2009-09-25 20:33:15)

Offline

#4 2009-09-25 20:56:36

anotherjohn
Member
Registered: 2009-09-23
Posts: 10

Re: [SOLVED]What is wrong with this invocation of prename?

Hi folks, thanks for your replies.

brisbin33: The -n option to prename makes it tell you what it would have done, without actually doing it. Also, your solution would add a leading '0' to the two-digit numbers as well, but thanks for trying smile

N-Acc wrote:

try escaping the `$1' in your regex or use single quotes.. otherwise it is intepreted by the shell.

We have a winner!

I can't *believe* I didn't try that. In fact, I'm pretty sure I did try that. Either way, it works now, thanks smile

[~] prename -n "s/_(\d)\.txt/_0\$1\.txt/" *
file_1.txt renamed as file_01.txt
file_2.txt renamed as file_02.txt
file_3.txt renamed as file_03.txt
file_4.txt renamed as file_04.txt
file_5.txt renamed as file_05.txt
file_6.txt renamed as file_06.txt
file_7.txt renamed as file_07.txt
file_8.txt renamed as file_08.txt
file_9.txt renamed as file_09.txt

Incidentally, 'echo' shows the problem immediately:

[~] echo prename -n "s/_(\d)\.txt/_0$1\.txt/"
prename -n s/_(\d)\.txt/_0\.txt/

[~] echo prename -n "s/_(\d)\.txt/_0\$1\.txt/"
prename -n s/_(\d)\.txt/_0$1\.txt/

Last edited by anotherjohn (2009-09-25 21:04:59)

Offline

#5 2009-09-25 21:33:00

brisbin33
Member
From: boston, ma
Registered: 2008-07-24
Posts: 1,799
Website

Re: [SOLVED]What is wrong with this invocation of prename?

anotherjohn wrote:

...but thanks for trying smile

john,

i'm glad you've got it solved, but please note:

you wrote:
[~] prename -n "s/_(\d)\.txt/_$1\.txt/" *
file_1.txt renamed as file_0.txt
file_2.txt renamed as file_0.txt
file_3.txt renamed as file_0.txt
file_4.txt renamed as file_0.txt
file_5.txt renamed as file_0.txt
file_6.txt renamed as file_0.txt
file_7.txt renamed as file_0.txt
file_8.txt renamed as file_0.txt
file_9.txt renamed as file_0.txt
me wrote:

in /this/ case, rename would've worked fine

i was very careful and purposely emphasized "in /this/ case" tongue.

Offline

#6 2009-09-25 21:42:25

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: [SOLVED]What is wrong with this invocation of prename?

anotherjohn wrote:

Also, your solution would add a leading '0' to the two-digit numbers as well

As `man rename` says, it can be used with ? instead of * for this reason.

Offline

#7 2011-08-17 16:03:41

Convergence
Member
Registered: 2005-07-02
Posts: 377

Re: [SOLVED]What is wrong with this invocation of prename?

I need to do the same operation that anotherjohn needed to do.  I don't have prename however, I thought that it was the same thing as perl-rename, so I installed that, but it didn't work.  Where can I find prename?


It's a very deadly weapon to know what you're doing
---  William Murderface

Offline

#8 2011-08-17 16:52:16

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,111
Website

Re: [SOLVED]What is wrong with this invocation of prename?

Old thread is old, perl-rename is prename, and "didn't work" is unhelpful. I suggest you start a new thread.

Offline

#9 2011-08-17 17:00:26

fsckd
Forum Fellow
Registered: 2009-06-15
Posts: 4,173

Re: [SOLVED]What is wrong with this invocation of prename?


aur S & M :: forum rules :: Community Ethos
Resources for Women, POC, LGBT*, and allies

Offline

Board footer

Powered by FluxBB