问题描述
我想给一个变量从一个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的
和我想要做的就是添加一个名为变量的内容版本
该文件包含唯一的字符串该行的最后一个字符之后 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的评论和upvoters使我确信它确实
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脚本:在文件的特定行的最后一个字符的文本追加的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!