I am looking for a BASH sed script that can open an .mdx
file and search line by line and find the second line that has the value I'm searching for: three hyphens like this ---
. Then, I'm hoping to insert two lines of redirect information above that second set of hyphens.
The second occurrence of these three hyphens could be on any line following line 1, so I would need a script that is smart enough to search until it finds the second one.
I'll need something that runs in the MacOS that can do some in-place file updates.
Here's my input file:
---
title: Some kind of title
---
I'd like to locate that second instance of three hyphens and insert new text above it like this:
---
title: Some kind of title
redirects:
- /some/kind/of/directory/path
---
In my shell script, I have a variable that contains that redirect path, so I would somehow need to pass that variable along with a hard-coded redirects:
to sed.
I looked at a variety of options, including the POSIX option included here, but it just deletes the second occurrence. Perhaps there's an easy way I could modify that to update?
Let me know if you need more to understand what I'm looking for.