2014-02-15 13 views
1

이것은 작년 말에 물어 본 질문의 단순화 된 버전이지만 그 밑으로 갈 수는 없습니다. 누군가이 설명으로 나를 도울 수 있기를 바랍니다.iTunes에서 내 보낸 XML 재생 목록이 잘못된 음악 구조의 파일을 참조합니다.

내 iTunes 재생 목록을 XML 파일 (songs.xml)로 외장 드라이브로 내보냈습니다. 내 보낸 각 노래는 메타 데이터가 XML 파일의 노드 아래에 저장되어있는 것처럼 보입니다. 2 개의 아델 (Adele) 노래가 포함 된 단편은 다음과 같습니다.

재생 목록을 내 보낸 후 음악 파일을 외부 드라이브의/Music 폴더로 복사했습니다. 문제는 모든 파일이 현재이 폴더에 있으며 하위 폴더에는 직접 존재하지 않는다는 것입니다. songs.xml 파일은 각 노래를/Music의 하위 폴더에있는 것으로 참조합니다./Music/Adele/21 -하지만 이제 더 이상 그렇지 않습니다 - 모든 파일은/Music에 있습니다. 따라서 노래를 다시 가져올 때 찾을 수 없습니다.

누군가가 songs.xml을 어떻게 구문 분석하고/Music/Artist/Album 참조를/Music으로 바꿀 수 있습니까? 그런 다음 파일에 설명 된대로 메타 데이터로 내 곡을 성공적으로 다시 가져올 수 있습니다. 추가 된 어려움은 음악/아티스트/앨범이 아닌 음악/아티스트 바로 아래에서 참조되는 노래가 있다는 것입니다. 예 : 아티스트는 '다양한'또는 편집 일 수 있습니다.

Mac 또는 Linux 터미널에 액세스하여 SED 나 RegEx 또는 조언 할 수있는 다른 명령을 실행할 수 있습니다. 너 내가 도와 주시면 아주 감사 할께.

미리 감사드립니다.

<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> 
+0

샘플 입력 또는 예상 출력 중 하나를 게시 한 것 같습니다 - 다른 쪽을 게시하십시오. –

답변

1

awk는 당신이 필요로 할 수있다.

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

그것은 라인의이 유형이 변경됩니다 :

<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> 

<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> 

에를 어떻게이 작품 :

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 
+0

EPIC !!! Jotne에게 감사 드려요. 나는이 awk에 대해 들어 본 적이 없지만, 당신의 논리와 결합하여 그 자리에 자리 잡고 있습니다. 다른 사람들은 이것을 사용하여> output.xml을 사용하여 출력을 새로운 XML 파일로 리디렉션했습니다. 감사합니다, 벤 – user3125872

+0

당신은 오신 것을 환영합니다. 'awk'는 리눅스 용 스위스 군 도구입니다. 거의 모든 것을 파일로 처리 할 수 ​​있습니다 :) – Jotne

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

사용하는 대신에 당신은 또한 다른 장소에 쉽게 적응하는 대신 Drive/Music의 변수를 사용하여 (이 경우에 나오지 행동 주위에 따옴표를 사용할 수

경로에 더 쉽게 읽고 /의 치료를위한 전통적인 /|