1

複数のセクションを含むテキスト ファイルがあり、それらのセクションの 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 ##

![](screeshot-1.png)
This is the first line

![](screeshot-2.png)
There could be blank lines like the one above and below.

![](screeshot-3.png)
Lines could contain special characters like ( # $ % etc and even quotes ""'` etc
![](screeshot-5.png)
Lines numbers need not be sequential.
![](screeshot-4.png)
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、一時ファイルの作成から逃れることができるという考えが浮かびました。

だから私の質問は、どのように呼び出すことができますsedawk? そうでない場合、このスクリプトを改善して一時ファイルの作成を回避する方法はありますか。

このスクリプトを Ubuntu と mac の両方で動作させたいことにご注意ください。ありがとう。

更新

1つの明確化。異なるセクションが常に同じ順序で存在する必要はありません。上記の例では、スクリーンショットの後に変更ログが続きますが、変更される可能性があります。私たちが知っている唯一のことは、次のセクションもで始まるということです##

更新 2 :

別の説明。スクリーンショット セクションには空白行が含まれる場合があり、その行は常に 2 語であるとは限りません。サンプルファイルを更新しました。

すべてのコーナーケースを事前に説明できなかったことを心からお詫び申し上げます。

4

3 に答える 3

4

1 つの簡単なawkスクリプト:

BEGIN {                 # Set the field separator to a . for picking up line num
    FS="."
}
/^##/ {                 # If we hit the a new section stop
    flag = 0
}
flag && $1~/^[0-9]+/ {  # If the the flag is set and starts with number add text
    print "![](screeshot-" $1 ".png)"
    sub(/^[0-9]+ /,"")  # Remove the leading line number (no limit on fields)
}
/^## Screenshots ##$/ { # If we hit the screenshot section start
    flag=1
}
{                       # Print all the lines in the file
        print
}

これをファイルに保存し、たとえばf.awk (おそらくもっと説明的なもの)、次のように実行します。

$ awk -f f.awk file

出力:

3.3行目
4.4行目

## スクリーンショット ##

![](スクリーンショット-1.png)
これが最初の行です

![](スクリーンショット-2.png)
上下のような空白行がある場合があります。

![](スクリーンショット-3.png)
行には ( # $ % などの特殊文字や引用符 ""'` などを含めることができます
![](スクリーンショット-5.png)
行番号は連続している必要はありません。
![](スクリーンショット-4.png)
行番号は任意の順序にすることができます


## 変更ログ ##

3.3行目
4.4行目
于 2013-10-17T09:43:40.357 に答える
3

あなたはこれを試すことができますsed

sed -r '/## Screenshots ##/,/## Changelog ##/{s/^([0-9]+)\. (.*)/![](screenshot-\1.png)\n\2/g}' yourfile

-r選択肢がない場合は、

sed '/## Screenshots ##/,/## Changelog ##/{s/^\([0-9]\+\)\. \(.*\)/![]\(screenshot-\1.png\)\n\2/g}' yourfile
于 2013-10-17T09:43:13.573 に答える
2

このワンライナーはあなたの問題に対してうまくいくはずです(あなたのアップデートで)

awk '/^## Screen/{p=1;print;next}p&&/^##/{p=0}p&&$0{print "![](screenshot-"++i".png)";$0=$2FS$3}7' file

出力:

3. line 3
4. line 4

## Screenshots ##

![](screenshot-1.png)
line 1
![](screenshot-2.png)
line 2
![](screenshot-3.png)
line 3
![](screenshot-4.png)
line 4

## Changelog ##

3. line 3
4. line 4

新しい例の編集:

awk '/^## Screen/{p=1;print;next}p&&/^##/{p=0}p&&$0{print "![](screenshot-"++i".png)";sub(/^\S* /,"")}7' file

出力:

3. line 3
4. line 4

## Screenshots ##

![](screenshot-1.png)
This is the first line

![](screenshot-2.png)
There could be blank lines like the one above and below.

![](screenshot-3.png)
Lines could contain special characters like ( # $ % etc and even quotes ""'` etc
![](screenshot-4.png)
line 4

## Changelog ##

3. line 3
4. line 4
于 2013-10-17T09:47:39.260 に答える