bash脚本:在文件的特定行的最后一个字符的文本追加 [英] Bash script: Appending text at the last character of specific line of a file

查看:88
本文介绍了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的和我想要做的就是添加一个名为变量的内容版本该文件包含唯一的字符串该行的最后一个字符之后 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脚本:在文件的特定行的最后一个字符的文本追加的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆