I have markdown files that contain YAML frontmatter metadata, like this:
---
title: Something Somethingelse
author: Somebody Sometheson
---
But the YAML is of varying widths. Can I use a Posix command like sed
to remove that frontmatter when it's at the beginning of a file? Something that just removes everything between ---
and ---
, inclusive, but also ignores the rest of the file, in case there are ---
s elsewhere.
If you don't mind the "or something" being perl.
Simply print after two instances of "---" have been found:
or a bit shorter if you don't mind abusing ?: for flow control:
Be sure to include
-i
if you want to replace inline.If you want to remove the front matter and only the front matter you could simply run:
If the first line doesn't match
---
,sed
willq
uit; else it willd
elete everything from the1
st line up to (and including) the next line matching---
(i.e. the entire front matter).I understand your question to mean that you want to remove the first
---
-enclosed block if it starts at the first line. In that case,This is:
Depending on how you want to handle lines that begin with
---abc
or so, you may have to change the patterns a little (perhaps add$
at the end to only match when the whole line is---
). I'm a bit unclear on your precise requirements there.you use a bash file, create
script.sh
and make it executable usingchmod +x script.sh
and run it./script.sh
.