英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论