Search code examples
replaceplaylist

Find, move, replace and parse strings simultanuosly while building an .xml playlist file


I get many videos and I need to compile functioning .xml playlist files where they are all listed, including snapshot jpg's. Videos and snapshot images are named automatically. So I end up with lots of files like this:

hxxp://site.com/video/_5712.480p.flv

hxxp://site.com/video/_5712.480p.jpg

hxxp://site.com/video/_5713.480p.flv

hxxp://site.com/video/_5713.480p.jpg

So with these files I need to produce an .xml file looking something like this:

....

    <track>
        <title>5712.480p</title>
        <creator>Whatever_5712.480p</creator>
        <info>hxxp://site.com/video/_5712.480p.jpg</info>
        <annotation>Playlist marked_480p</annotation>
        <location>hxxp://site.com/video/_5712.480p.flv</location>
        <image>hxxp://site.com/video/_5712.480p.jpg</image>

    </track>
    <track>
        <title>5713.480p</title>
        <creator>Whatever_5713.480p</creator>
        <info>hxxp://site.com/video/_5713.480p.jpg</info>
        <annotation>Playlist marked_480p</annotation>
        <location>hxxp://site.com/video/_5713.480p.flv</location>
        <image>hxxp://site.com/video/_5713.480p.jpg</image>

    </track>

So I guess I might be looking at some advanced sed/awk procedure to copy, move and place the right strings inside the correct brackets, and to compile one whole file? I really appreciate all the help I can get on this one. Thx


Solution

  • With that input, you can do something like:

    awk 'NR%2==1 && /\.jpg$/ {JPGFILE=$0}
         NR%2==0 { print "whateverXMLtags" JPGFILE "whatanotherXMLtags" $0 "someotherXMLtags" }' INPUTFILELIST
    

    So this assumes that jpg files are on odd numbered lines, and on that saves the name, and on every even line prints the desired output. Note that the SPACE between e.g. JPGFILE and "whatanotherXMLtags" concatenates the sring.