2

How to rename the last number-string it finds with a zero-padded version and leave the rest of the filename alone?

AAA 2000-01-03 bbb 2001-03-04 page_1.pdf

To:

AAA 2000-01-03 bbb 2001-03-04 page_0001.pdf

See:

Eliah Kagan
  • 117,780
Rafael
  • 105

1 Answers1

1

The simplest thing would probably be to match the preceding page_ string

Ex:

$ rename -n 's/page_(\d+)/sprintf "page_%04d", $1/e' *.pdf
rename(AAA 2000-01-03 bbb 2001-03-04 page_1.pdf, AAA 2000-01-03 bbb 2001-03-04 page_0001.pdf)

(remove the -n when you are happy that it is doing the right thing).


If you really need to match the "last number", you could use positive lookahead to anchor the match to the end of the string $ with a possibly empty sequence of non-digits between:

$ rename -n 's/(\d+)(?=\D*$)/sprintf "%04d", $1/e' *.pdf
rename(AAA 2000-01-03 bbb 2001-03-04 page_1.pdf, AAA 2000-01-03 bbb 2001-03-04 page_0001.pdf)
steeldriver
  • 136,215
  • 21
  • 243
  • 336