本文介绍了Bash 脚本:在文件特定行的最后一个字符处附加文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在来自 bash 脚本的文件特定行的最后一个字符处附加一个变量.

I am trying to append a variable at the last character of a specific line of a file from a bash script.

该文件名为 myfile.txt,我想要做的是将名为 VERSION 的变量的内容附加到该行的最后一个字符之后包含唯一字符串 MYVERSION 的文件.

The file is called myfile.txt and what I want to do is to append the contents of a variable named VERSION right after the last character of the line of the file that contains the unique string MYVERSION.

也就是说,如果在这一行中有以下内容:

That is, if in this line there is the following:

MYVERSION=0.1

VERSION="-custom_P1" 那么,我想要以下内容:

and VERSION="-custom_P1" then, I want to have the following:

MYVERSION=0.1-custom_P1

谢谢大家的帮助.

推荐答案

试试这个:

sed -i "/^MYVERSION=/ s/\$/$VERSION/" myfile.txt

这个想法是找到以 MYVERSION= 开头的行,然后用 $VERSION 环境变量的内容替换该行的结尾.

The idea is that it finds a line that starts with MYVERSION= and then replaces the end of that line with the contents of the $VERSION environment variable.

最初我不确定第一个 $ 是否需要转义,但@sehe 的评论和它的支持者让我相信它确实如此.

originally I wasn't sure if the first $ needed to be escaped, but @sehe's comment and its upvoters convinced me that it does.

这篇关于Bash 脚本:在文件特定行的最后一个字符处附加文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 07:52