次の構成ファイルが提供されます。
shape {
visible: true
type: rectangle
...
}
shape {
type: circle
visible: isRound === "true"
...
}
shape {
// comment: "visible" not set, default "true"
}
コメントに触れずにキー(プロパティ)を使用してすべての値を設定し、bash
以前の値をコメントに保つにはどうすればよいですか?新しいコンテンツは次のようになります。visible
false
shape {
visible: false
type: rectangle
...
}
shape {
type: circle
visible: false // visible: isRound === "true"
// ideally, above, the old value is kept as a comment...
...
}
shape {
visible: false
// comment: "visible" not set, default "true"
}
ファイルは単純なshape
構造リストではなく、他の項目も含めることができます。これはおそらく品質管理言語遵守する。
awk
示す:
awk --version
GNU Awk 5.2.1, API 3.2
Copyright (C) 1989, 1991-2022 Free Software Foundation.
編集する
私はこれを使用できると思いますPCRE regex
:
(\s*shape\s*\{\n)(\s*)(.*?)(visible\:.*\n?)?(.*?)(\n\s*\})
代替式は次のとおりです。
$1$2visible: false // $4\n$2$5$6
または
\1\2visible: false // \4\n\2\5\6
ただし、これを適用するにはツールが必要です。完璧ではなく、まだ2回レビューする必要はありません。
ベストアンサー1
perl
この種の仕事に私が使用するもの:
perl -0777 -pe '
s{shape\s*\{.*?\}}{
$& =~ s{(//.*)|\b(visible:\s*+)(?!true\s*$).*}{$1 ? $1 : "${2}true // $&"}gmre
}gse' your-file