Domanda

This is a simplified version of a question I asked at the end of last year but could not get to the bottom of it. I hope that somebody can help me with this explanation.

I exported my iTunes playlist as an XML file (songs.xml) onto an external drive. Each song exported appears to have its metadata stored under a node in the XML file. A fragment containing 2 Adele songs is below.

After exporting the playlist, I copied the music files to the /Music folder on the external drive. The issue is that ALL files are now directly in this folder and not within the subfolders. The songs.xml file references each song as being in a subfolder of /Music e.g. /Music/Adele/21 - but that is no longer the case - all files are in /Music. Thus when I attempt to import the songs back in they cannot be found.

Can somebody tell me how I can parse songs.xml and replace the /Music/Artist/Album references with just /Music ? Then I could successfully re-import my tunes with their metadata as described in the file! An added difficulty is that some songs are referenced just under the Music/Artist, and not Music/Artist/Album. e.g. the Artist could be 'Various' or a compilation.

I can get access to a Mac or Linux terminal to run SED or a RegEx or any other command that you can advise. If you can help I'd be very grateful.

Thanks a lot in advance.

Ben

<dict>
  <key>Track ID</key><integer>22041</integer>
  <key>Name</key><string>Rolling in the Deep</string>
  <key>Artist</key><string>Adele</string>
  <key>Album Artist</key><string>Adele</string>
  <key>Album</key><string>21</string>
  <key>Persistent ID</key><string>B123AA625019E726</string>
  <key>Track Type</key><string>File</string>
  <key>Purchased</key><true/>
  <key>Location</key><string>file://localhost/Volumes/External%20Hard%20Drive/Music/Adele/21/RollingInTheDeep.m4a</string>
  <key>File Folder Count</key><integer>1</integer>
  <key>Library Folder Count</key><integer>1</integer>
</dict>
<dict>
  <key>Track ID</key><integer>22042</integer>
  <key>Name</key><string>Someone Like You</string>
  <key>Artist</key><string>Adele</string>
  <key>Album Artist</key><string>Adele</string>
  <key>Album</key><string>Someone Like You</string>
  <key>Persistent ID</key><string>A274ED723536E610</string>
  <key>Track Type</key><string>File</string>
  <key>Purchased</key><true/>
  <key>Location</key><string>file://localhost/Volumes/External%20Hard%20Drive/Music/Adele/SomeoneLikeYou.mp3</string>
  <key>File Folder Count</key><integer>1</integer>
  <key>Library Folder Count</key><integer>1</integer>
</dict>
È stato utile?

Soluzione

This awk may be what you need.

awk '/Drive\/Music/ {sub(/\/string/,":string");sub(/Music.*\//,"Music/");sub(/:string/,"/string")}1' file

It will change this type of lines:

  <key>Location</key><string>file://localhost/Volumes/External%20Hard%20Drive/Music/Adele/21/RollingInTheDeep.m4a</string>
  <key>Location</key><string>file://localhost/Volumes/External%20Hard%20Drive/Music/Adele/SomeoneLikeYou.mp3</string>

to

  <key>Location</key><string>file://localhost/Volumes/External%20Hard%20Drive/Music/RollingInTheDeep.m4a</string>
  <key>Location</key><string>file://localhost/Volumes/External%20Hard%20Drive/Music/SomeoneLikeYou.mp3</string>

How does this work:

awk '
/Drive\/Music/ {                    # Serch for all lines with Drive/Music lines
    sub(/\/string/,":string")       # Replace last / to prevent problem with greedy regex in next step
    sub(/Music.*\//,"Music/")       # Replace from Music to last / with only Music/ (using .* greedy)
    sub(/:string/,"/string")        # Replace last / back to its original
    }
    1                               # Print all lines, changed and not changed
    ' file                          # input file

Altri suggerimenti

sed '/Location/ s|\(Drive/Music\)[^<]*\(/[^/<]*<\)|\1\2|' YourFile

use of | instead of traditional / for easier reading and treatment of / in path

you could also use variable instead of Drive/Music to adapt easily to another place (and use double quote around the sed action in this case

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top