Reading content between markers
Say we want to read some file contents between a set of markers, like the frontmatter of a markdown document, for example:
blog-post.md
We can use sed
to read the content contained within the ---
markers with:
We're telling sed
to print all lines in the range defined by matches of the pattern. If we want to exclude the markers themselves, we can delete them by extending the command:
This works well for piping the output to another tool, like yq
:
However, we run into problems if the marker used occurs in other places within the document. In markdown, ---
can be used as a horizontal divider:
blog-post.md
If we want to only read the frontmatter, awk
is more suitable. To start off with, we can achieve something similar to the sed
solution using a flag:
If we want to print only the frontmatter, we need something else:
After reading the second occurence of the marker, awk
will skip the rest of the file.
Side note
If the starting pattern and ending pattern are different, we can use another solution:
Last updated