Skip to content

Instantly share code, notes, and snippets.

@ajshell1
Last active July 5, 2021 15:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ajshell1/a6b3905b475e9c0f18c42790d8f1e5aa to your computer and use it in GitHub Desktop.
Save ajshell1/a6b3905b475e9c0f18c42790d8f1e5aa to your computer and use it in GitHub Desktop.
datslim
#!/bin/bash
#depends on xmlstarlet
#Proper usage: ./datslim name-of-datfile
# This assumes you already have a list of dumps called "have"
#Step 1: assign file names to variables. This is not needed, but it's a habit of mine.
#if you don't like
inhave=have
havefile=have2
tempdat=tempdat.xml
#"$1" is bash's shorthand for "command line argument #1".
newdat="new_$1"
#These next steps set up the first few lines of the new dat. Quick and dirty, but it works
echo '<?xml version="1.0"?>' > "$newdat"
echo '<!DOCTYPE datafile PUBLIC "-//Logiqx//DTD ROM Management Datafile//EN" "http://www.logiqx.com/Dats/datafile.dtd">' >> "$newdat"
echo '<datafile>' >> "$newdat"
#This uses xmlstarlet to copy the data in the .dat header into the new dat.
xml sel -t -c "/datafile/header" "$1">> "$newdat"
#these next few lines use sed to fix the problems I've been having with apostrophes and ampersands.
#Apostrophes screw up quoting, so I replace them temporarily with "£". In theory, any other character that doesn't
# appear in the title of any game should work. This first line fixes them in the have file
sed -e "s/'/£/g" -e 's/\&amp\;/\&/g' "$inhave" > "$havefile"
#this next line changes apostrophes to £ in a temporary dat file.
sed "s/'/£/g" "$1" > "$tempdat"
#This line counts the amount of lines in your have file
havecount=$(wc -l have2 | cut -f1 -d' ')
#Here we start a for loop. We loop through the lines in the have file, one by one.
for f in $(seq 1 "$havecount"); do
#This awk statement takes the contents of line f in "have" and puts it in g.
g=$(awk 'NR == n' n="$f" "$havefile")
#This is where the magic happens. It uses xmlstarlet to print out any values in our datfile where the game name is
#the same as our current value in f. Then it reverses the £ to apostrophe swap.
xml sel -t -c "/datafile/game[@name='$g']" "$tempdat" | sed -e "s/&#xA3;/'/g" -e "s/£/'/g" >> "$newdat"
echo "$g"
done
#this finishes off the datfile
echo "</datafile>" >> "$newdat"
#remove temporary files
rm "$havefile" "$tempdat"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment