🛠️
Notes
  • README
  • ansible
    • Literal curly braces (raw)
  • bash
    • Case statement
    • Change quote style mid-string
    • Comparing versions
    • Hiding credentials on the command line
    • Directory of script
    • Using find to run a command on multiple files
    • Getting the line number of the nth match
    • Getopts
    • Parsing output with long lines using less
    • Print line at number
    • Remove final newline
    • Reading content between markers
    • Determine if a script was sourced or executed
    • Bash substring
    • Run a function on interrupt or error
    • Reference variable by name
  • chrome
    • Bypassing Chrome's NET::ERR_CERT_INVALID page
  • css
    • box-sizing: border-box
    • The currentColor keyword
    • Wrapper taking up at least 100% height
    • Using margin: auto; for centering
    • Margin-collapse
    • Which unit to use?
    • Hiding elements
  • git
    • Conditional git config includes
    • Viewing the evolution of a line or function
    • Name of current branch
    • Get the path to the repository root
    • Replaying a set of changes on a specific branch
    • Hide file from git diff output
    • Listing untracked files with git status
  • github
    • Setting up GitHub Actions
  • groovy
    • Appending items to a list
  • kubernetes
    • Waiting for a pod to be ready
  • make
    • Passing arguments to make rules
    • Running make in a set of subdirectories
  • npm
    • Update a value in a project's .npmrc
  • podman
    • Target last container
  • rust
    • The match operator
    • Unwrapping a Result
  • tmux
    • New window with prompt
  • tools
    • Running ngrok in the background
    • Using entr to react to file changes
  • typescript
    • Inferring the type of elements in an array
  • vim
    • The command-line window
    • Populate quickfix list with eslint errors
    • Visual increment
    • Opening a list of files in split windows
    • Insert line above matched line
    • Spelling
    • The tabular plugin
    • Populate quickfix list with tsc errors
  • yaml
    • Yaml multiline strings
  • zsh
    • Lazy loading command setup
Powered by GitBook
On this page

Was this helpful?

  1. bash

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

---
title: My first blog post
tags:
  - blog
  - first
  - thing
---

# Welcome to my blog

Lots of interesting content.

We can use sed to read the content contained within the --- markers with:

sed -n '/^---$/,/^---$/p' blog-post.md

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:

sed -n '/^---$/,/^---$/ { /^---$/d; p; }' blog-post.md

This works well for piping the output to another tool, like yq:

sed -n '/^---$/,/^---$/ { /^---$/d; p; }' blog-post.md | yq eval '.title' -

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

---
title: My first blog post
tags:
  - blog
  - first
  - thing
---

# Welcome to my blog

Lots of interesting content.

---

A completely separate topic.

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:

awk '/^---$/{flag =! flag; next}flag' blog-post.md

If we want to print only the frontmatter, we need something else:

awk '/^---$/{if (flag == 0) {flag = 1;next} else {exit}}flag' notes/bash-substring-contains.md

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:

awk '/start/{flag=1;next}/end/{flag=0}flag'
PreviousRemove final newlineNextDetermine if a script was sourced or executed

Last updated 4 years ago

Was this helpful?