You are not logged in.
Pages: 1
Topic closed
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
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 . hopefully a prename user will come along and tell you why what you did didn't work.
//github/
Offline
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
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
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
[~] 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
...but thanks for trying
john,
i'm glad you've got it solved, but please note:
[~] 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
in /this/ case, rename would've worked fine
i was very careful and purposely emphasized "in /this/ case" .
//github/
Offline
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
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
aur S & M :: forum rules :: Community Ethos
Resources for Women, POC, LGBT*, and allies
Offline
Pages: 1
Topic closed