如何在Shell中用多行文本替换特定行

huangapple go评论72阅读模式
英文:

How to replace a specific line with multiline text in shell

问题

I have line uses: actions/checkout@v3.5.0 (note the indentation at the beginning) which needs to be replaced with multiline text

      uses: actions/checkout@v3.5.0
    - name: Set node version
      run: |-
        echo "NODE_VERSION=$(jq -r '.engines.node // "latest"' package.json)" >> $GITHUB_ENV
    - uses: actions/setup-node@v3
      with:
        node-version: ${{ env.NODE_VERSION }}
    - name: Setup project-level .npmrc file and Run Node18 config fix
      run: |
        curl -u ${{ secrets.CICD_ARTYLAB_PUBLISH_USER }}:${{ secrets.CICD_ARTYLAB_PUBLISH_TOKEN }} --fail "https://someurl/api/npm/auth" >> ${{ env.npm_config_userconfig }}
        if [[ $NODE_VERSION -ge 18 ]]; then
          npm config --location project fix
        fi

I did try placing the multiline text in a file and using that in sed. But didn't work.

sed "s| uses: actions/checkout@v3.5.0|$content|g" edmg-ui-master.yml

Getting below error.

sed: 1: "s| uses: actions/c ...": unescaped newline inside substitute pattern

Any other ideas to do this?

英文:

I have line uses: actions/checkout@v3.5.0 (note the indentation at the beginning) which needs to be replaced with multiline text

      uses: actions/checkout@v3.5.0
    - name: Set node version
      run: |-
        echo "NODE_VERSION=$(jq -r '.engines.node // "latest"' package.json)" >> $GITHUB_ENV
    - uses: actions/setup-node@v3
      with:
        node-version: ${{ env.NODE_VERSION }}
    - name: Setup project-level .npmrc file and Run Node18 config fix
      run: |
        curl -u ${{ secrets.CICD_ARTYLAB_PUBLISH_USER }}:${{ secrets.CICD_ARTYLAB_PUBLISH_TOKEN }} --fail "https://someurl/api/npm/auth" >> ${{ env.npm_config_userconfig }}
        if [[ $NODE_VERSION -ge 18 ]]; then
          npm config --location project fix
        fi

I did try placing the multiline text in a file and using that in sed. But didn't work.

sed "s| uses: actions/checkout@v3.5.0|$content|g" edmg-ui-master.yml

Getting below error.

> sed: 1: "s| uses: actions/c ...": unescaped newline inside substitute pattern

Any other ideas to do this?

答案1

得分: 1

这可能对您有用(GNU sed):

sed -E '/uses: actions\/checkout@v3\.5\.0/{x;s/.*/cat contentFile/e
        :a;G;/^(.+)\n(\s*).*/{s///;P;s/[^\n]*(\n|$)//;ba};x;d}' file

这将匹配所需的字符串,将新内容复制到保持缓冲区中,然后将前导空格插入保持缓冲区的每一行。

在处理过程中打印结果并删除原始行。

注:新内容保存在单独的文件中,例如 contentFile,可能也包含缩进。

英文:

This might work for you (GNU sed):

sed -E '/uses: actions\/checkout@v3\.5\.0/{x;s/.*/cat contentFile/e
        :a;G;/^(.+)\n(\s*).*/{s///;P;s/[^\n]*(\n|$)//;ba};x;d}' file

This matches the required string, copies the new content into the hold buffer, then inserts the leading white space into each line of the hold buffer.

Prints the result as it processes and deletes the original line.

N.B. The new content is held in a separate file e.g. contentFile and may contain indentation too.

huangapple
  • 本文由 发表于 2023年6月12日 15:40:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76454496.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定