Bash script: Appending text at the last character of specific line of a file

前端 未结 2 1161
情书的邮戳
情书的邮戳 2021-01-15 01:13

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

The file is called myfile.txt and what I want to

相关标签:
2条回答
  • 2021-01-15 01:41

    Try

    sed -e "s/^MYVERSION=/MYVERSION=.*$/&${VERSION}/g" < myfile.txt
    

    The command appends the value of VERSION to the line with 'MYVERSION='

    0 讨论(0)
  • 2021-01-15 01:53

    Try this:

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

    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.

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

    0 讨论(0)
提交回复
热议问题