Linux tutorial: sed replaces lines starting with a certain string

If you only want to match TAG=lines starting with as a replacement rule, you can slightly modify sedthe command's replacement expression. Here is the updated example script:

#!/bin/bash

target_value="2.0.0.RELEASE"  # 目标值

# 使用sed命令替换以TAG=开头的行中的字符串
sed -i 's/^TAG=.*/TAG='"$target_value"'/g' .env

In the updated replacement expression, we use ^TAG=.*to match TAG=lines starting with . This will match any TAG=line starting with and replace it with TAG=目标值.

Make sure to replace the filename in the script .envwith the filename you actually use and target_valueset it to your desired target value. After running the script, it will replace the string .envin the file starting TAG=with the target value.

Guess you like

Origin blog.csdn.net/a772304419/article/details/132905612