0

I have a file containing a list of information looks something like this:

#FILENAME "Some name - some title.xml"    
<song>some song</song>
<year>1994.</year>
<album>Some album</album>
<artist>some artist</artist>
#FILENAME "another filename - some have ' in title title.xml"
<song>another song</song>
<year>1996.</year>
<album>another album</album>
<artist>another artist</artist>
#FILENAME "yet another filename - something.xml"
...
..
.

with more than 25 000 lines I need to create separate files (5000 files.xml) so first line is FILENAME second to fifth lines are informations that need to be fields in xml file that looks like this:

<?xml version='1.0' encoding='UTF-8'?><Metadata>    <artist></artist>    <song></song>    <album></album>    <year></year></Metadata>

Can someone help me with the script?

So far I have removed # form FILENAME from the document and done something like this:

But can't manage to create multiple files

#!/bin/bash



while read line; 
if  [[ $line == FILENAME* ]]; then
     filename="${line:9}"

fi
if [[ $line == *artist*  ]]; then
    artist=$line
fi
if [[ $line == *song* ]]; then
    song=$line
fi
if [[ $line == *album* ]]; then
    album=$line
fi
if [[ $line == *year* ]]; then
    year=$line
fi

do

    echo "<?xml version='1.0' encoding='UTF-8'?><Metadata>    $artist    $song    $album    $year</Metadata>"

done < popis.txt > $filename
1
  • What is the output of your script? Commented Sep 17, 2019 at 0:51

1 Answer 1

0
  1. You could split your file popis.txt into 5000+ temporary files with the split (GNU coreutils) command:

    split -d -a4 -l5 popis.txt split
    

    This creates the files split0001, split0002, ... with five lines per file and makes further processing a bit easier.

  2. Create this modified script of yours and save it as script.sh:

    #!/bin/bash
    
    for file; do
      while read -r line; do
        if [[ "$line" = "<artist>"* ]]; then
          artist=$line
        elif [[ "$line" = "<song>"* ]]; then
          song=$line
        elif [[ "$line" = "<album>"* ]]; then
          album=$line
        elif [[ "$line" = "<year>"* ]]; then
          year=$line
        else
          # remove prefix `#FILENAME "` and the last quote `"`
          filename=$(echo "$line" | sed 's/[^"]*"//;s/"[[:space:]]*$//')
        fi
      done < "$file"
      echo "<?xml version='1.0' encoding='UTF-8'?><Metadata>${artist}${song}${album}${year}</Metadata>" > "$filename"
    done
    
  3. Make your script executable and run the script on all splitXXXX files:

    chmod +x script.sh
    ./script.sh split*
    
  4. If everything went well, this should have created one XML file for each input file and you can remove the temporary files:

    rm split*
    
1
  • Thanks, it worked.. Commented Sep 17, 2019 at 2:38

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.