Skip to content

Instantly share code, notes, and snippets.

@dajare
Last active October 4, 2025 11:57
Show Gist options
  • Save dajare/19a417d56b866314a076c16db3bca875 to your computer and use it in GitHub Desktop.
Save dajare/19a417d56b866314a076c16db3bca875 to your computer and use it in GitHub Desktop.
Bash script to titlecase chapter titles in UPPERCASE
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
@dajare
Copy link
Author

dajare commented Nov 27, 2023

COMMENTARY - This assumes:

  • the title is in line 12 of the file, in <p>...</p> delimiters
  • the title itself begins at "column" 8 (i.e., immediately after the opening <?> tag; h3 would begin at 9-)
  • it assumes *nix sed regex (tested on Ubuntu); might need adjusting for MacOS
  • convenient checking of results available by uncommenting line 5.

Test 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:

for filename in chapter-*.xhtml; do
    tctitle=$(sed -n 's/.*title\">\(.*\)/\1/p' "$filename" | se titlecase -n)
    sed -i -e 's#^\(.*title\">\).*#'"\1$tctitle"'#g' "$filename"
  done

@dajare
Copy link
Author

dajare commented Apr 18, 2025

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

@dajare
Copy link
Author

dajare commented Apr 19, 2025

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.)

@dajare
Copy link
Author

dajare commented Oct 4, 2025

This script (produced by copilot) copies the value of the <title> wrapper into the h2 wrapper, in-place, for all *.xhtml files in directory:

To modify all *.xhtml files in-place within a directory, you can extend the script like this:

#!/bin/bash

# Loop through all .xhtml files in the current directory
for file in *.xhtml; do
    # Extract the title content
    title=$(sed -n 's|.*<title>\(.*\)</title>.*|\1|p' "$file")

    # Replace the <h2> content with the title, in-place
    sed -i -E "s|<h2>.*</h2>|<h2>$title</h2>|" "$file"
done

How it works:

  • for file in *.xhtml: loops through each XHTML file.
  • sed -n 's|.*<title>\(.*\)</title>.*|\1|p': extracts the title text.
  • sed -i -E: edits the file in-place (-i) using extended regex (-E).
  • s|<h2>.*</h2>|<h2>$title</h2>|: replaces the <h2> content with the extracted title.

Optional enhancements:

  • Add a backup option: sed -i.bak -E ... to keep .bak files.
  • Add a check to skip files without <title> or <h2>.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment