Skip to content

Instantly share code, notes, and snippets.

@dazzag24
Last active October 20, 2020 14:58
Show Gist options
  • Save dazzag24/d553f89539926f3ee41251c27d888957 to your computer and use it in GitHub Desktop.
Save dazzag24/d553f89539926f3ee41251c27d888957 to your computer and use it in GitHub Desktop.
Rename TV episode files containing _1_-_3 pattern to S01E03 style
# Using the rename tool - note that this is unable to pad the series and episode to two digits
rename -n 's/^Blah_([0-9])_-_([0-9])\.([a-zA-Z_])/Blah_S$1E$2\.$3/' *.mp4
#rename(Blah_1_-_3._Foo_Bar_p01fmqt1_default.mp4, Blah_S1E3._Foo_Bar_p01fmqt1_default.mp4)
rename -n 's/^Orphan_Black_Series_([0-9])_-_([0-9])\.([a-zA-Z_])/Orphan_Black_Series_S$1E$2\.$3/' *.mp4
#rename(Orphan_Black_Series_1_-_1._Natural_Selection_p01fmqt1_default.mp4, Orphan_Black_Series_S1E1._Natural_Selection_p01fmqt1_default.mp4)
# Or using awk
ls Orphan_Black_Series_1_-_10._Endless_Forms_Most_Beautiful_b03fvlhc_default.mp4 | awk '{a=gensub( /^([^0-9]+)_Series_.*/, "\\1","1"); b=gensub( /^[^0-9]+_Series_([0-9]+).*/, "\\1","1"); c=gensub( /^[^0-9]+_Series_[0-9]+_-_([0-9]+).*/, "\\1","1");d=gensub( /^[^0-9]+_Series_[0-9]+_-_[0-9]+\.(.*)$/, "\\1","1")
;printf "a=%s\nb=%s\nc=%s\nd=%s\n", a,b,c,d}'
#a=Orphan_Black
#b=1
#c=10
#d=_Endless_Forms_Most_Beautiful_b03fvlhc_default.mp4
ls Orphan_Black_Series_1*.mp4 | awk '{a=gensub( /^([^0-9]+)_Series_.*/, "\\1","1"); b=gensub( /^[^0-9]+_Series_([0-9]+).*/, "\\1","1"); c=gensub( /^[^0-9]+_Series_[0-9]+_-_([0-9]+).*/, "\\1","1");d=gensub( /^[^0-9]+_Series_[0-9]+_-_[0-9]+\.(.*)$/, "\\1","1") ;printf "mv %s %s_S%02dE%02d%s\n", $0,a,b,c,d}'
#mv Orphan_Black_Series_1_-_1._Natural_Selection_p01fmqt1_default.mp4 Orphan_Black_S01E01_Natural_Selection_p01fmqt1_default.mp4
#mv Orphan_Black_Series_1_-_10._Endless_Forms_Most_Beautiful_b03fvlhc_default.mp4 Orphan_Black_S01E10_Endless_Forms_Most_Beautiful_b03fvlhc_default.mp4
# Final version. Pipe the output of the awk printf into bash to actually run the commands.
ls Orphan_Black_Series_1*.mp4 | awk '{a=gensub( /^([^0-9]+)_Series_.*/, "\\1","1"); b=gensub( /^[^0-9]+_Series_([0-9]+).*/, "\\1","1"); c=gensub( /^[^0-9]+_Series_[0-9]+_-_([0-9]+).*/, "\\1","1");d=gensub( /^[^0-9]+_Series_[0-9]+_-_[0-9]+\.(.*)$/, "\\1","1") ;printf "mv %s %s_S%02dE%02d%s\n", $0,a,b,c,d}' | bash
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment