How to remove the original YAML line from markup files?

I have markdown files containing FrontMatter YAML metadata, for example:

---
title: Something Somethingelse
author: Somebody Sometheson 
---

But YAML has a different width. Can I use the Posix command, for example sed, to remove this front mass when it is at the beginning of the file? Something that simply deletes everything between ---and ---, inclusive, but also ignores the rest of the file, if there ---is another place.

+5
source share
5 answers

I understand that your question means that you want to delete the first ----included block if it starts on the first line. In this case

sed '1 { /^---/ { :a N; /\n---/! ba; d} }' filename

It:

1 {              # in the first line
  /^---/ {       # if it starts with ---
    :a           # jump label for looping
    N            # fetch the next line, append to pattern space
    /\n---/! ba; # if the result does not contain \n--- (that is, if the last
                 # fetched line does not begin with ---), go back to :a
    d            # then delete the whole thing.
  }
}
                 # otherwise drop off the end here and do the default (print
                 # the line)

, , ---abc , , (, $ , , ---), .

+8

, :

sed '1{/^---$/!q;};1,/^---$/d' infile

---, sed q uit; d 1 ( ) , --- (.. ).

+4

" -", perl.

"---":

perl -ne 'if ($i > 1) { print } else { /^---/ && $i++ }' yaml

, ?: :

perl -ne '$i > 1 ? print : /^---/ && $i++' yaml

-i, .

+1
source

You use the bash file, create script.shand make it executable with, chmod +x script.shand run it ./script.sh.

#!/bin/bash

#folder articles contains a lot of markdown files
files=./articles/*.md

for f in $files;
do
    #filename
    echo "${f##*/}"
    #replace frontmatter title attribute to "title"
    sed -i -r 's/^title: (.*)$/title: "\1"/' $f
    #...
done
0
source

This AWK-based solution works with files with and without FrontMatter, without doing anything further.

#!/bin/sh
# Strips YAML FrontMattter from a file (usually Markdown).

# Exit immediately on each error and unset variable;
# see: https://vaneyckt.io/posts/safer_bash_scripts_with_set_euxo_pipefail/
set -Ee

print_help() {
    echo "Strips YAML FrontMattter from a file (usually Markdown)."
    echo
    echo "Usage:"
    echo "    'basename $0' -h"
    echo "    'basename $0' --help"
    echo "    'basename $0' -i <file-with-front-matter>"
    echo "    'basename $0' --in-place <file-with-front-matter>"
    echo "    'basename $0' <file-with-front-matter> <file-to-be-without-front-matter>"
}

replace=false
in_file="-"
out_file="/dev/stdout"

if [ -n "$1" ]
then
    if [ "$1" = "-h" ] || [ "$1" = "--help" ]
    then
        print_help
        exit 0
    elif [ "$1" = "-i" ] || [ "$1" = "--in-place" ]
    then
        replace=true
        in_file="$2"
        out_file="$in_file"
    else
        in_file="$1"
        if [ -n "$2" ]
        then
            out_file="$2"
        fi
    fi
fi

tmp_out_file="$out_file"
if $replace
then
    tmp_out_file="${in_file}_tmp"
fi

awk -e '
BEGIN {
    is_first_line=1;
    in_fm=0;
}
/^---$/ {
    if (is_first_line) {
        in_fm=1;
    }
}
{
    if (! in_fm) {
        print $0;
    }
}
/^(---|...)$/ {
    if (! is_first_line) {
        in_fm=0;
    }
    is_first_line=0;
}
' "$in_file" >> "$tmp_out_file"

if $replace
then
    mv "$tmp_out_file" "$out_file"
fi
0
source

All Articles