🧠 Second Brain

Search

Search IconIcon to open search

Counting Words written in Markdown

Last updated Feb 9, 2024

To document the progress of my book writing, I created a little script that outputs words written over time.

The main counting is done with wc -w, as ChatGPT told me:

1
find src -type f -not -path '*/\.*' -name '*.md' -exec cat {} \; | wc -w

Interesting is to count the words over time. As I have everything in git, I loop over git logs with this script words-history.sh:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
#!/bin/bash

last_count=0

# Iterate over each commit
git log --reverse --pretty=format:"%H %ad" --date=short | while read hash date; do
    # Check out the commit
    git checkout $hash &> /dev/null

    # Count the words in markdown files
    words=$(find src -type f -not -path '*/\.*' -name '*.md' -exec cat {} + | wc -w)
    
    # Compare with the last count and output only if different
    if [ "$words" -ne "$last_count" ]; then
        echo "$date,$words"
        last_count=$words
    fi
done

# Check out the original branch
git checkout main

Now I can run below in my Makefile:

1
./words-history.sh > stats/stats-words.csv

With Rill Developer, I imported and created CSV and made a dashboard with

1
2
rill init
rill start

Simple is beauty.


Origin:
References: Markdown
Created 2023-11-09