我有包含 YAML frontmatter 元数据的 Markdown 文件,如下所示:
---
title: Something Somethingelse
author: Somebody Sometheson
---
但 YAML 的宽度各不相同。我可以使用 Posix 命令吗sed
当它位于文件开头时删除该 frontmatter 吗?只是删除之间的所有内容的东西---
and ---
,包含在内,但也会忽略文件的其余部分,以防有---
在其他地方。
我理解你的问题意味着你想删除第一个---
- 封闭块(如果从第一行开始)。在这种情况下,
sed '1 { /^---/ { :a N; /\n---/! ba; d} }' filename
This is:
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
或者这样,你可能需要稍微改变一下模式(也许添加$
最后仅当整行是时才匹配---
)。我有点不清楚你的具体要求。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)