2017-01-10 4 views

答えて

1

レスキューへのPerl!

perl -lne 'my @replacement = ("First line", "Second line"); 
      if ($p = (/^---$/ .. /^## \[Unreleased\]/)) { 
       print $replacement[$p-1]; 
      } else { print }' 

フリップフロップオペレータ..は、さらに、それは範囲に対する相対行番号を返し、次の2つの文字列の間だかどうかを示します。

0
awk -v RS="\0" 'gsub(/---\n\n## \[Unreleased\]\n/,"something")+1' file 

この行に試してみてください。

+0

!ありがとうございました。 –

0

この例は役に立ちます。

$ cat f 
Some text 
--- 

## [Unreleased] 
More text here 

$ seq 1 5 >mydata.txt 

$ cat mydata.txt 
1 
2 
3 
4 
5 

$ awk '/^---/{f=1; while(getline < c)print;close(c);next}/^## \[Unreleased\]/{f=0;next}!f' c="mydata.txt" f 
Some text 
1 
2 
3 
4 
5 
More text here 
1

これは(GNUのSED)あなたのために働くかもしれない:

sed '/^---/,/^## \[Unreleased\]/c\something else' file 

変更に必要な文字列には、2つの正規表現の間のラインを。

+0

うまくやった。 BSD 'sed'で動作させるには、' c \ 'の後に明示的な改行が必要です。 'bash'の' $ '...'文字列を使用します: 'sed -e $ '/^--- /、/^## \\ [未リリース\\]/c \\\ nsomething else' file' – mklement0

0

awkソリューション:

  • がポータブルである(POSIX準拠)。
  • は、ブロックの開始行と終了行の間の任意の数の行を扱うことができます(複数のブロックを同じテキストで置き換えることもできます)。
  • は、(ファイル全体を一度に読むのではなく)1行ずつファイルを読み取ります。私が探していたまさに
awk -v new='something else' ' 
/^---$/ { f=1; next }        # Block start: set flag, skip line 
f && /^## \[Unreleased\]$/ { f=0; print new; next } # Block end: unset flag, print new txt 
! f             # Print line, if before or after block 
' file 
関連する問題