copy certain parts of a filename (shell?)

Discussion in 'Mac Programming' started by kangping, Mar 29, 2011.

  1. kangping macrumors member

    Joined:
    Mar 30, 2010
    #1
    hi there,

    i am trying to get hazel to automate some file sorting for me. right now i am doing the following:

    1. tag files using sublerCLI with this code
    Code:
    #! /bin/sh 
    
    # media kind attribute
    mediaKind="TV Show";
    
    # media kind attribute
    TVShow="King of Queens";
    
    #Artist
    Artist="Kevin James, Leah Remini, Jerry Stiller, Victor Williams, Patton Oswalt";
    
    #Genre
    Genre="Comedy";
    
    /Applications/SublerCLI -i "$1" -O -t {Media' 'Kind:"$mediaKind"}{TV' 'Show:"$TVShow"}{Artist:"$Artist"}{Genre:"$Genre"};
    2. send the whole thing to itunes and delete the original file.

    what i would like to do is change metadata for title. the original file looks like this (coming from a online recorder):
    Code:
    king_of_queens_name_of_episode_1111_2222.mp4
    how can i extract "name of episode", delete the underscores and put it into a variable? the loop of course is handled by hazel.

    thanks, i hope its clear what i want to do :)

    dave
     
  2. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #2
    Does your recorder let you tweak that naming scheme?

    Otherwise I don't see a delimiter the computer can zero in on between the show and episode names. The show could be called king or king_of_queens_name for all the computer knows. If you can make the delimiter between them different like maybe _-_ you might have a better shot at automating this.

    B
     
  3. kangping thread starter macrumors member

    Joined:
    Mar 30, 2010
    #3
    thanks for the fast reply. unfortunately this is a online service, which does not have to many options for naming conventions. i was hoping i could kind of "count" what the computer should delete...

    for example (this is an actual name containing tv show, episode, date, time airing and some counter):

    King_of_Queens_Latin_Lover_28-03-2011_1500_391678.mp4

    - delete the first 15 characters
    - delete the last 21 characters
    - in this case: and the extension
    - change "_" to " "
    - copy the remaining characters to $EpisodeTitle

    or something like that :) my knowledge is just big enough to be dangerous. haha.

    thanks for the help up to now :)
    dave
     
  4. frogdesign macrumors newbie

    Joined:
    Jan 5, 2010
    #4
    Here's my first try using sed, I still need a way to handle mixed case:
    For a loop you'd have: for F in `ls *.mp4' do; echo $F; done

    > ls *.mp4
    king_of_queens_name_of_episode_1111_2222.mp4

    >TVShow="king or queens"

    > t=`ls *.mp4 | sed 's/\(.*\)\.mp4/\1/' | tr "_[0-9]" " "`

    > echo ${TVShow#$t}
    king or queens
     
  5. sero macrumors member

    Joined:
    Aug 28, 2008
    #5
    This would work for the filename you gave

    Code:
    FileName=King_of_Queens_Latin_Lover_28-03-2011_1500_391678.mp4
    EpisodeName=$(echo "$FileName"|cut -d"_" -f4-|rev|cut -d"_" -f4-|rev|sed s/_/\ /g)
    echo "$EpisodeName"
    This would have to be adjusted if the naming convention of the file was different, i.e., the name of the show is more/less then 3 words, or number of fields after the episode name is different. You could also use awk, also using "_" as a separator...

    I would personally keep the "_" but that's just me.
     
  6. kangping thread starter macrumors member

    Joined:
    Mar 30, 2010
    #6
    thanks for the great answer sero.
    would this also have to be adjusted, if the word count of the episode title would change? if you don't mind you could decode the line you wrote - i would like to understand what is happening. otherwise... you only used sed? then i will rtfm ;)

    edit: about the sepreator: this is not actually for a file name, but just the episode title i want to use as a metatag. unfortunately the metatag has the full tv show + episode title in it :)


    thanks,
    dave
     
  7. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #7
    Might I suggest an alternative approach then?

    If you have a tag that includes the "TV Show" and a tag that includes "TV Show + Episode Name" why not use "TV Show + Episode Name" - "TV Show" to get what you want without the additional episode ID numbers etc.. at the end?

    B
     
  8. sero macrumors member

    Joined:
    Aug 28, 2008
    #8
    The episode name can be whatever. Basically what's happening is the beginning and the end of the file name are being chopped off, leaving whatever is in the middle (episode name).

    Cut is using "_" to separate the file name into fields - first we print the fourth field to the end, leaving "Latin_Lover_28-03-2011_1500_391678.mp4". Then "rev" reverses this, giving "4pm.876193_(etc)" and we repeat with cut, leaving "revoL_nitaL". Then this is reversed again with "rev" and finally sed replaces any instances of "_" with a space.
     
  9. kangping thread starter macrumors member

    Joined:
    Mar 30, 2010
    #9
    hi sero,

    thanks for the reply and the great explenation. i would have never gotten the idea to mirror the whole thing :)

    it's getting late here in china, so i'll hit the bed. i'll have a go at it tomorrow and respond :)

    greetings,
    dave
     
  10. kangping thread starter macrumors member

    Joined:
    Mar 30, 2010
    #10
    hi there again,

    the script works just great. i'm using it on several different "hazel jobs" now. thanks a lot.

    dave
     

Share This Page