Last active
April 19, 2025 16:43
-
-
Save dajare/19a417d56b866314a076c16db3bca875 to your computer and use it in GitHub Desktop.
Bash script to titlecase chapter titles in UPPERCASE
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for filename in chapter-*.xhtml; do | |
new12=$(sed -n '12p' "$filename" | cut -b 8- | se titlecase -n) | |
sed -i -e '12s#^\(.*<p>\).*#'"\1$new12"'#g' "$filename" | |
## Uncomment next line to echo new title line to terminal for checking | |
# sed -n '12p' "$filename" | |
done |
Also, with titles on separate lines of a text file, this will work:
while read line; do
se titlecase "$line"
done < titles.txt
Sample line from titles.txt
=
input: A Title Of A Story
| output: A Title of a Story
HT: Superuser
This doesn't change case: it takes the filename, truncates the extension, saves that string as variable, and does a search/replace
with sed
. E.g., for short story file: make the id="..."
to id=~~
to make delimiting search easier:
ls | while read filename; do
full="${filename}"
id="${full%.*}"
sed -i "s/~~/$id/g" ${filename}
done;
(The dquo
deliimiters with sed
are required for variable replacement in bash
.)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
COMMENTARY - This assumes:
<p>...</p>
delimiters<?>
tag;h3
would begin at9-
)sed
regex (tested on Ubuntu); might need adjusting for MacOSTest data can conveniently be found in this Github repo/commit (ZIP can be downloaded; contains 75 chapters with uppercase titles).
For "post semantics", then this variation "works": it identifiles the line with the
epub:type="title"
semantic, and does the titlecase replacement on that line: