So I'm trying to do a simple regex replacement of a date in the metadata of a document using sed from bash. For example, suppose I have input file test.md
containing:
---
title: "I am a file"
date: December 1, 2021
---
Loren ipsum blah blah blah
I'd like to be able to run a bash script on December 29 and get an output file
---
title: "I am a file"
date: December 29, 2021
---
Loren ipsum blah blah blah
So here's my first try:
#!/bin/bash
TODAY=$(date +'%B %d, %Y')
STARTBIT="date: "
FULLDATE="$STARTBIT$TODAY"
REGEX="s/date:\s.*\n/$FULLDATE/"
echo $REGEX # to make sure I'm getting what I think I'm getting
sed -e $REGEX < test.md > output.md
but I get the following output:
s/date:\s.*\n/date: December 29, 2021/
sed: 1: "s/date:\s.*\n/date:
": unescaped newline inside substitute pattern
so this is a bit confusing, the first line is my echoed pattern, and I definitely don't see any newlines in it on the command line. Nor am I sure quite where newlines would supposedly be??
So then I thought, ok, maybe the newline is appended to the end of one of the variables, and for some reason is made invisible due to some bash silliness when I echo it. So based on this prior SO answer, I just went in and stripped newlines from the end of everything just to make sure. Viz:
#!/bin/bash
TODAY=$(date +'%B %d, %Y')
STARTBIT="date: "
CLEANSTARTBIT=${STARTBIT%%[[:space:]]}
CLEANTODAY=${TODAY%%[[:space:]]}
FULLDATE="$STARTBIT$TODAY"
CLEANFULLDATE=${FULLDATE%%[[:space:]]}
REGEX="s/date:\s.*\n/$CLEANFULLDATE/"
CLEANREGEX=${REGEX%%[[:space:]]}
echo $CLEANREGEX
sed -e $CLEANREGEX < test.md > output.md
and I'm still getting exactly the same output. But now I'm really stumped. There can't possibly be newlines sneaking in here...
Help??
Bonus possible issues:
I'm using the version of sed that shipped with macOS. Heaven only knows what version. Maybe I should try getting my hands on GNU sed??
I don't really know what flavor of regex sed uses, or indeed how sed works at all... I basically just copied the regex over from the one I was using in a python script since forever, for learning purposes/because I'm sick of calling out to python for this bit of basic text processing that I do all the time. Hah, but I actually know python regex...