awk 複数行の変更

awk 複数行の変更

次の datadog ファイルがあります

########################################
## System Probe Network Configuration ##
########################################

# network_config:
  ## @param enabled - boolean - optional - default: false
  ## Set to true to enable the Network Module of the System Probe
  #
  # enabled: falseee



##########################################
## Security Agent Runtime Configuration ##
##                                      ##
## Settings to sent logs to Datadog are ##
## fetched from section `logs_config`   ##
##########################################

# runtime_security_config:
  ## @param enabled - boolean - optional - default: false
  ## @env DD_RUNTIME_SECURITY_CONFIG_ENABLED - boolean - optional -
default: false
  ## Set to true to enable Cloud Workload Security (CWS).
  #
  # enabled: falseee

主に次の部分を変換したい...

########################################
## System Probe Network Configuration ##
########################################

# network_config:
  ## @param enabled - boolean - optional - default: false
  ## Set to true to enable the Network Module of the System Probe
  #
  # enabled: falseee

これを入力してください

########################################
## System Probe Network Configuration ##
########################################

# network_config:
  ## @param enabled - boolean - optional - default: false
  ## Set to true to enable the Network Module of the System Probe
  #
  # enabled: TRUE

ファイルの有効化:エラーに複数行があるため、複数行のawk検索を使用して検索を実行していますが、実際にファイル全体を変更できないように見えました。

検索パターンを作成しました。

alias start="# network_config:"
alias end="# enabled: falseee"

その後、awkを使って次のように目的の部分を切り取ろうとしました。

main=$(awk "/$start/, /$end/" system.yaml)

そして私が本当に変えたい部分は...

sub_value=$(awk "/$start/, /$end/" system.yaml | awk '{sub(/enabled: falseee/,"enabled: TRUE"); print}')

だから今、ファイル全体でこの機能を切り替えるには、次のようにします。しかし、うまくいかないようです。

echo $file | awk -v srch="$main" -v repl="$sub_value" '{ sub(srch,repl,$0); print $0 }'

でも

echo "$(awk 'awk "{sub(/$(awk "/$start/, /$end/" system.yaml)/, $sub_value); print}"' system.yaml)" > newFile

awkを使用してこれを実行したいのは、職場でtaniumを使用し、すべてのサーバーに対してこのファイルを構成する方法がわからないためです。だから私はサーバーごとのファイルブロックを変更するためにtaniumを介してスクリプトを実行したいと思います。私の考えでは、awkのソースに陥っているようです。何を?

ベストアンサー1

もし許可される:

perl -00pe 's/(# network_config.*?enabled: )falseee/$1true/s' file

出力

########################################
## System Probe Network Configuration ##
########################################

# network_config:
  ## @param enabled - boolean - optional - default: false
  ## Set to true to enable the Network Module of the System Probe
  #
  # enabled: true

編集したい場合所定の位置に-iスイッチを追加:

perl -i -00pe .....

おすすめ記事