找到匹配的文本并替换yml中的下一行

时间:2019-03-29 10:28:22

标签: linux awk sed

我正在尝试在yml配置文件中找到一行并将下一行替换为特定值。我尝试使用sed,但似乎无法替换或无法找到该模式。以下是该yml文件的摘要

<div>
    <input autocomplete="email" data-val="true" data-val-length="Votre adresse e-mail doit contenir 70 caractères ou moins" data-val-length-max="70" data-val-regex="Veuillez entrer une adresse e-mail valide" data-val-regex-pattern="^.+@.+\..+$" data-val-required="S'il vous plaît entrer votre adresse e-mail" id="Account_Email" maxlength="70" name="Account.Email" placeholder="Name@domain.com" required="required" type="text" value="" aria-required="true" aria-invalid="true" aria-describedby="Account_Email-error">
</div>
<span class="form-validation-message field-validation-error" data-valmsg-for="Account.Email" data-valmsg-replace="true">
    <span id="Account_Email-error" class="has-error">Field is required.</span>
</span>

我想将 applicationConnectors port 值更改为 14081 ,因为存在另一个 port > adminConnectors 执行脚本后,它应如下所示:

  applicationConnectors:
    - type: http
      port: 14080
      bindHost: 15.213.48.154
      headerCacheSize: 512 bytes
      outputBufferSize: 32KiB
      maxRequestHeaderSize: 8KiB
      maxResponseHeaderSize: 8KiB
      inputBufferSize: 8KiB
      idleTimeout: 30 seconds
      minBufferPoolSize: 64 bytes
      bufferPoolIncrement: 1KiB
      maxBufferPoolSize: 64KiB
      acceptorThreads: 1
      selectorThreads: 2
      acceptQueueSize: 1024
      reuseAddress: true
      useServerHeader: false
      useDateHeader: true
      useForwardedHeaders: true

  adminConnectors:
    - type: http
      port: 14180

我尝试了以下代码:

  applicationConnectors:
    - type: http
      port: 14081
      bindHost: 15.213.48.154
      headerCacheSize: 512 bytes
      outputBufferSize: 32KiB
      maxRequestHeaderSize: 8KiB
      maxResponseHeaderSize: 8KiB
      inputBufferSize: 8KiB
      idleTimeout: 30 seconds
      minBufferPoolSize: 64 bytes
      bufferPoolIncrement: 1KiB
      maxBufferPoolSize: 64KiB
      acceptorThreads: 1
      selectorThreads: 2
      acceptQueueSize: 1024
      reuseAddress: true
      useServerHeader: false
      useDateHeader: true
      useForwardedHeaders: true

  adminConnectors:
    - type: http
      port: 14180

但是看来这段代码并不能代替任何东西。

2 个答案:

答案 0 :(得分:1)

由于端口线是applicationConnectors:之后的第二行,因此您需要使用双n;,并且应在sed命令周围使用双引号,以允许在内部进行变量插值:

sed "/applicationConnectors:/{n;n;s/\($var2\).*/\1: ${var1}/}" configuration.yml > newfile

请参阅此online sed demo

答案 1 :(得分:1)

sed最适合s / old / new,仅此而已。对于其他任何事情,只需使用awk即可获得清晰性,可移植性,鲁棒性等。

$ awk -v rec='applicationConnectors' -v tag='port' -v val='14081' '
    /^  [^ ]/{name=$1} name==(rec":") && $1==(tag":"){sub(/[^ ]+$/,""); $0=$0 val}
1' file
  applicationConnectors:
    - type: http
      port: 14081
      bindHost: 15.213.48.154
      headerCacheSize: 512 bytes
      outputBufferSize: 32KiB
      maxRequestHeaderSize: 8KiB
      maxResponseHeaderSize: 8KiB
      inputBufferSize: 8KiB
      idleTimeout: 30 seconds
      minBufferPoolSize: 64 bytes
      bufferPoolIncrement: 1KiB
      maxBufferPoolSize: 64KiB
      acceptorThreads: 1
      selectorThreads: 2
      acceptQueueSize: 1024
      reuseAddress: true
      useServerHeader: false
      useDateHeader: true
      useForwardedHeaders: true

  adminConnectors:
    - type: http
      port: 14180

是否想将acceptQueueSize:更改为17?这是相同的脚本,只是变量值不同:

$ awk -v rec='applicationConnectors' -v tag='acceptQueueSize' -v val='17' '
    /^  [^ ]/{name=$1} name==(rec":") && $1==(tag":"){sub(/[^ ]+$/,""); $0=$0 val}
1' file
  applicationConnectors:
    - type: http
      port: 14080
      bindHost: 15.213.48.154
      headerCacheSize: 512 bytes
      outputBufferSize: 32KiB
      maxRequestHeaderSize: 8KiB
      maxResponseHeaderSize: 8KiB
      inputBufferSize: 8KiB
      idleTimeout: 30 seconds
      minBufferPoolSize: 64 bytes
      bufferPoolIncrement: 1KiB
      maxBufferPoolSize: 64KiB
      acceptorThreads: 1
      selectorThreads: 2
      acceptQueueSize: 17
      reuseAddress: true
      useServerHeader: false
      useDateHeader: true
      useForwardedHeaders: true

  adminConnectors:
    - type: http
      port: 14180

如果您喜欢计算sed,请尝试使用当前接受的n解决方案:-)。还要注意,无论行在记录中以何种顺序出现,这都是行得通的,因为它会键入名称port,而不是假设行将在applicationConnectors:之后出现特定数量的行。最后,即使您要搜索或替换的字符串包含RE元字符(例如.,反向引用字符(例如\1&)或sed分隔符(例如/)。