Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the 'a sed script for reformatting .toc files' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
a sed script for reformatting .toc files
Authored by: ChrisR on Apr 04, '04 04:05:08AM

I use cdrdao to rip the table of contents from CDs so that I can store it with my archived FLAC files. Problem is, the file that cdrdao creates contains more information than I care about - all I really want is the pregap information. sed to the rescue! The following script will only output the bare minimum information needed to burn a CD with pregaps identical to the source CD:


s/^CD_DA$/CD_DA\
/p;/^TRACK AUDIO$/p;/^FILE/{h;N;s/\(.*\)\n\(.*\)/\2\
\1/;s/^\n\(.*\)/\1/;s/^START/PREGAP/;s/FILE "\(.*\)".*/FILE "\1" 0\
/p;g;}

Save this script into a file named "striptoc" (or whatever you like) and use the following command to apply the changes to "old.toc" and save them to "new.toc":


sed -n -f striptoc old.toc > new.toc

Alternately, you could put it into a unix shell script for ease-of-use:


#!/bin/sh
if [ "$1" ]
then
  disktool -u disk1
  cdrdao read-toc --device IODVDServices --driver generic-mmc "$1"
  if [ -e "$1" ]
  then
    sed -n 's/^CD_DA$/CD_DA\
/p;/^TRACK AUDIO$/p;/^FILE/{h;N;s/\(.*\)\n\(.*\)/\2\
\1/;s/^\n\(.*\)/\1/;s/^START/PREGAP/;s/FILE "\(.*\)".*/FILE "\1" 0\
/p;g;}' "$1" | sed '$d' > "$1".new
    mv "$1".new "$1"
  else
    echo "No such file: "$1""
    exit 1
  fi
else
  echo "Usage: "$(basename "$0")" <tocfile>"
  exit 1
fi

Now simply type 'striptoc cd.toc' and it will read the table of contents from your inserted CD and save it to "cd.toc". You will still need to edit the file by hand to add the filenames for the .wav files that you want to burn onto the CD.

[ Reply to This | # ]

useless use of hold space
Authored by: ChrisR on Apr 09, '04 09:55:14AM

The use of the hold space in the above code is completely unnecessary. Here's how the script should look:


#n
s/^CD_DA$/CD_DA\
/p
/^TRACK AUDIO$/p
/^FILE/{
N
s/\(.*\)\n\(.*\)/\2\
\1/
s/^\n\(.*\)/\1/
s/^START/PREGAP/
s/FILE "\(.*\)".*/FILE "\1" 0\
/p
}

Run the script with 'sed -f SEDSCRIPT > TOCFILE'

[ Reply to This | # ]

final update
Authored by: ChrisR on Apr 12, '04 09:32:05PM

Okay, here's my final update to the striptoc script. I ported it over to awk so that I could bring in a listing of flac files from the current directory.


#!/usr/bin/awk -f
BEGIN { print "CD_DA\n" }
{ FS = "\n"; RS = ""
  if ($2 == "TRACK AUDIO") {
    print $2
    if ($NF ~ /^START/) { 
      sub(/^START/, "PREGAP", $NF)
      print $NF
    }
    FS = " "; RS = "\n"
    "ls *.flac" | getline file
    sub(/flac$/, "wav", file)
    print "FILE \"" file "\" 0\n"
  }
}


[ Reply to This | # ]