複数のセクションを含むテキスト ファイルがあり、それらのセクションの 1 つ (私の場合は ## Screenshots ## セクション) 内にあるコンテンツを変更し、他のセクションはそのままにしておきたいと考えています。
ファイルの一部は次のようになります
3. line 3
4. line 4
## Screenshots ##
1. This is the first line
2. There could be blank lines like the one above and below.
3. Lines could contain special characters like ( # $ % etc and even quotes ""'` etc
5. Lines numbers need not be sequential.
4. Lines numbers could be in any order
## Changelog ##
3. line 3
4. line 4
に変更する必要があります
3. line 3
4. line 4
## Screenshots ##

This is the first line

There could be blank lines like the one above and below.

Lines could contain special characters like ( # $ % etc and even quotes ""'` etc

Lines numbers need not be sequential.

Lines numbers could be in any order
## Changelog ##
3. line 3
4. line 4
それを行うには、次のスクリプトがあります。
awk '/^## Screenshots ##/ {f="screenshot.md";print > f;next}
f=="screenshot.md" && /^##/ {f="after_screenshot.md"}
{print > f}' f=before_screenshot.md readme.md
sed "/^[0-9]/s/\(^[0-9]*\)./\!\[\](screeshot-\1.png)\\`echo -e '\n\r'`/" screenshot.md > processed-screenshots.md
cat before-screeshots.md processed-screenshots.md after-screenshots.md > readme.md
できます。しかし、ご覧のとおり、非常に見苦しく、複数の一時ファイルを作成するこの不要なステップもあります。
私はこれを改善できるかどうかを確認しようとしていましたが、何らかの方法でsed
から呼び出すことができればawk
、一時ファイルの作成から逃れることができるという考えが浮かびました。
だから私の質問は、どのように呼び出すことができますsed
かawk
? そうでない場合、このスクリプトを改善して一時ファイルの作成を回避する方法はありますか。
このスクリプトを Ubuntu と mac の両方で動作させたいことにご注意ください。ありがとう。
更新:
1つの明確化。異なるセクションが常に同じ順序で存在する必要はありません。上記の例では、スクリーンショットの後に変更ログが続きますが、変更される可能性があります。私たちが知っている唯一のことは、次のセクションもで始まるということです##
更新 2 :
別の説明。スクリーンショット セクションには空白行が含まれる場合があり、その行は常に 2 語であるとは限りません。サンプルファイルを更新しました。
すべてのコーナーケースを事前に説明できなかったことを心からお詫び申し上げます。