我有包含YAML frontmatter元数据的markdown文件,如下所示:
---
title: Something Somethingelse
author: Somebody Sometheson
---
Run Code Online (Sandbox Code Playgroud)
但是YAML的宽度各不相同.我可以使用Posix命令sed来删除文件开头处的那个前端吗?东西只是删除一切之间---和---,包容性,也忽略了文件的其余部分,如果有---小号在其他地方.
我理解你的问题意味着---如果它从第一行开始,你想删除第一个-enclosed块.在这种情况下,
sed '1 { /^---/ { :a N; /\n---/! ba; d} }' filename
Run Code Online (Sandbox Code Playgroud)
这是:
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)
Run Code Online (Sandbox Code Playgroud)
根据你想要处理以---abc左右开头的行的方式,你可能需要稍微改变一下模式(可能$在末尾添加只在整行时匹配---).我对你的确切要求有点不清楚.