LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-26-2021, 01:30 PM   #1
snowman81
Member
 
Registered: Aug 2006
Location: Michigan
Distribution: Ubuntu
Posts: 282

Rep: Reputation: 30
Understanding rename/sprintf


I'm having trouble understanding the following statement. I am renaming a directory of files which are episodes of a TV show where the format is The.Blah.Season.01.Episode.01.-.Pilot.mp4

The line I am using is this
Code:
rename -n -- 's/.*(\d+).*(\d+)/sprintf "S%02dE%02d.mp4", $1, $2/e' *.mp4
So the -n is a dry run and doesn't make a change. The
Code:
s/.*(\d+).*(\d+)
looks like it's using sed and a regex that finds the first and second occurrence of digits. The last part just does all these actions on any mp4 in the directory. The middle bit about sprintf I know controls the season/episode numbers but I'm getting confused when I try to look it up. Is the C version different from Java and so on?

When I run the command it looks like it sequentially increases the season number and the episode number is wrong. That first file shows up as S01E04.mp4

A) I don't understand where the part is that makes the season number increase.

and

B) Is there a way to keep the show name and episode name and only have the season and episode number show up as xxx.S01.E01.xxx
 
Old 02-26-2021, 01:42 PM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,356
Blog Entries: 3

Rep: Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767
It is a perl script and any perl-based regular expression or even any normal perl expression can be processed.

So, it's not getting incremented, the \d+ is getting fetched from a different part of the file name than you think. The .* is greedy, and thus the 4 in the mp4 is $2 in your pattern.

Code:
touch The.Blah.Season.01.Episode.03.-.Pilot.mp5
rename -n -- 's/.*(\d+).*(\d+)/sprintf "S%02dE%02d.mp4", $1, $2/e' *.mp5
 
2 members found this post helpful.
Old 02-26-2021, 03:22 PM   #3
snowman81
Member
 
Registered: Aug 2006
Location: Michigan
Distribution: Ubuntu
Posts: 282

Original Poster
Rep: Reputation: 30
I appreciate your help and you were totally correct. To be honest, I was waaaay overthinking the issue. I ended up doing it like this:

Code:
rename -n -- 's/eason\.//g' *.mp4
rename -n -- 's/pisode\.//g' *.mp4
 
  


Reply

Tags
bash, rename command



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
sprintf to c++ conversion rino.caldelli Programming 2 03-03-2006 09:44 PM
sprintf value disappear syseeker Programming 8 02-17-2006 04:20 AM
How to print the "%m" "%d" using sprintf command ? stny Linux - Software 1 02-08-2006 09:55 PM
programming-segmentation fault(sprintf) ramakrishna sura Linux - General 1 12-20-2005 04:12 AM
sprintf not working in char driver module snigglyfox Programming 2 04-27-2004 01:25 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:36 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration