Markdownファイルからタイトルを取得し、特定の文字を削除する方法

Markdownファイルからタイトルを取得し、特定の文字を削除する方法

以下の例のようにMarkdownファイルがあります。

---
id: http
title: Title can be anything
sidebar_label: HTTP API
---
import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';

export let Bubble = ({ item }) => {
    return (
        <div>
            <div style={{ display: 'flex', fontFamily: 'monospace', borderRadius: '3px', backgroundColor: '#ddd', display: 'inline', padding: '5px'}}>
                {item}
            </div>
            <div className=twentypx/>
        </div>
    );
}

## This title can be anything too
bla bla

各ファイルには---との間に異なる内容があります##。タイトルを取得したいので、---上記の内容を次のよう##に変更したいと思います。# Title can be anything

したがって、目的の最終結果は次のようになります。

# Title can be anything

## This title can be anything too
bla bla

他のファイルのタイトルは異なります。 Bashでこれを行うにはどうすればよいですか?

ベストアンサー1

使用sed

$ sed -E '/title:/{s/[^ ]* (.*)/\1/;h};/^-/,/^}/d;/^$/{x;s/^/# /;G}' input_file
# HTTP API

## General & Auth
bla bla

おすすめ記事