gpt4 book ai didi

text-processing - 如何从markdown文件中删除YAML主题?

转载 作者:行者123 更新时间:2023-12-04 16:52:33 25 4
gpt4 key购买 nike

我有包含YAML前题元数据的markdown文件,如下所示:

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

但是YAML的宽度各不相同。当文件开头时,可以使用 sed这样的Posix命令删除该文件吗?只是删除了 ------之间的所有内容(包括两端),但会忽略文件的其余部分,以防其他地方存在 ---

最佳答案

我理解您的问题是指,如果要从第一行开始,删除第一个--- -enclosed块。在这种情况下,

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

这是:
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开头的行的方式,您可能需要稍作更改(也许在末尾添加 $以仅在整行是 ---时才匹配)。我不清楚您在那里的确切要求。

关于text-processing - 如何从markdown文件中删除YAML主题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28221779/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com