Counting tags for my Yearly Review
January 25, 2025 —
Nico Cartron
This is very ugly and probably only usable by me, but here's a Shell script I quickly wrote to save me from manually counting the number of articles per tag on my blog, which uses BashBlog.
I use it for my "year in review" blog posts moving forward.
#!/bin/sh
YEAR=`date +%Y`
FOLDER=year_recap
TAGS=$FOLDER/tags_$YEAR.txt
TAGS_UNIQ=$FOLDER/tags_uniq_$YEAR.txt
TAGS_UNIQ_SPLIT=$FOLDER/tags_uniq_split_$YEAR.txt
TAGS_UNIQ2=$FOLDER/tags_uniq2_$YEAR.txt
if [ -e $TAGS ]
then
rm $TAGS
fi
if [ -e $TAGS_UNIQ ]
then
rm $TAGS_UNIQ
fi
if [ -e $TAGS_UNIQ2 ]
then
rm $TAGS_UNIQ2
fi
# First we get a list of all the tags for the past year
# We exclused Tags pages, as well as all posts and index.
# We use `html2text` since Markdown files are sometimes in the past, so I'm leaning on HTML files instead
for i in `find . -name "*.html" -type f -mtime -365 -maxdepth 1|egrep -v 'tag_|all_tags.html|all_post.html|index.html'`; do cat $i|grep "^<p>Tags"|cut -d: -f2 |html2text >> $TAGS; done
# Then we sort them and display unique
cat $TAGS|sort|uniq > $TAGS_UNIQ
# Then we split tags for article with multiple tags
for i in `cat $TAGS_UNIQ`; do j=`echo $i|sed -e s/,//`; echo $j >> $TAGS_UNIQ_SPLIT ;done
# Then we again |sort |uniq
cat $TAGS_UNIQ_SPLIT|sort|uniq > $TAGS_UNIQ2
# And finally we show how many articles per tag
for i in `cat $TAGS_UNIQ2`; do COUNT=`grep $i $TAGS|wc -l`; echo $i:$COUNT;done
Tags: Misc