#!/bin/bash # Add terzina tags to each canto.xml file in a directory # Also add canto number attribute. echo -e "\nAdding terzina tags and numbers." for file in *; do if [[ "$file" != 'terzina-tags.sh' ]]; then # exclude this script if [[ "$file" != sed* ]]; then # exclude sed file made later echo -e "Modifying $file" # Every third line (mod 3) add a closing tag, newline, and opening tag. awk -i inplace '{print}; NR % 3 == 0 {print "\t\t\n\t\t"}' "$file" # Add an opening tag at the beginning and a closing tag at the end because the above awk doesn't. sed -i '1s/^/\t\t\n/' "$file" echo -e "\t\t" >> "$file" # Add the canto number attribute (at most 160/3 ~ 54 terzine) for i in $(seq 1 54); do # Substitute in the actual number with sed. I couldn't figure out # how to include the $i variable with the surrounding attribute # quotes in the awk statement, so I just used sed here separately. # We start at i=2 because the awk command above # doesn't add a terzina tag to the first terzina. TERZ_NUM=$((i + 1)) # Replace only first occurence of ~NUM~. sed -i -e "0,/~NUM~/ s/~NUM~/\"$TERZ_NUM\"/" "$file" done fi fi done