The script: split_manuscript.sh
#!/bin/bash
#split_manuscript.sh: splits a txt file into Chapters and Scenes
chapnum='1' #for starting the loop
#make a directory for the chapter and go into it
mkdir -p "$1"
cd "$1"
#split the manuscript into chapters
awk -v RS="CHAPTER\n" 'NR > 1 { print $0 > "Chapter_" (NR-1) ".txt"; close("Chapter_" (NR-1) ".txt") }' "$1"".txt"
#get the number of chapters
maxnumchaps=`ls -1 | grep -i .*.txt | wc -l`
while [ $chapnum -le $maxnumchaps ] ; do
#split scenes within a chapter
mkdir -p "Chapter_"$chapnum
cd "Chapter_"$chapnum
awk -v RS="\* \* \*\n" 'NR > 1 { print $0 > "Scene_" (NR-1) ".txt"; close("Scene_" (NR-1) ".txt") }' "../Chapter_"$chapnum".txt"
cd ..
#only leave the 'chapter' info in the Chapter_n.txt file
echo 'CHAPTER' | cat - "Chapter_"$chapnum".txt" > temp && mv temp "Chapter_"$chapnum".txt"
awk '/END/{flag=0}flag;/CHAPTER/{flag=1}' "Chapter_"$chapnum".txt" > temp && mv temp "Chapter_"$chapnum".txt"
#increment so we can do the next chapter
chapnum=$(( $chapnum + 1 ))
done
How to use
The script can be run as a Zim ‘custom tool’ (Tools –> Custom Tools) like this: [path to location of script]/split_manuscript.sh %d. Name the tool ‘Split Manuscript’ and put whatever you like for the description. Paste your manuscript file into a Zim page or manually drop it into the appropriate spot in the directory structure. In Zim, right-click on the page, select 'Split Manuscript' from the right-click menu and sit back and wait for a while. If only it was really that easy. Okay, it is that easy IF your manuscript has exactly the right formatting.
Formatting rules
- The opening word of your manuscript file must be 'CHAPTER'
- Immediately following the word 'CHAPTER' must be the specified scene break sequence: '* * *'
- All scenes must be divided using '* * *'
awk -v RS="\* \* \*\n" 'NR > 1 { print $0 > "Scene_" (NR-1) ".txt"; close("Scene_" (NR-1) ".txt") }' "../Chapter_"$chapnum".txt"
For example, if you wanted to use the # character, you'd change "\* \* \*\n" to "#\n". Enjoy :).
No comments:
Post a Comment