Conditional Batch File Editing

Adopted Mmark for Math Posts

Problem

During the adoption of Mmark for math posts on this blog, I had to insert markup: mmark at the last line of front matter of the source file of each math post.

Seek help

I separated this into two SO questions

  1. Sed conditional match and execute command with offset, and
  2. A question about AWK multiple line recognition.

Solution

From #1., I learnt the use of variables in AWK scripts. From #2, some users explained how these variables can be used for multi-line regex search.

Here’s the actual command ran.

find content/post/**/*.md -print0 |
while IFS= read -d '' -r file; do
awk '
/^- math$/ {
  if(lastLine == "categories:") {
    f=1
  }
}
{ lastLine = $0 }
/^---$/ && f { print "markup: mmark"; f=0 }
1
' $file > ${file%.*}.bak
done
awk  sed  Mmark 

No comment

Your email address will not be published. Required fields are marked *.