sed 在匹配后用变量替换第 3 行

问题描述 投票:0回答:1

我有一个这样的文件:

[profile.one]
username=sam
password=secret
email=something

[profile.two]
username=kate
password=secret
email=something

[profile.three]
username=mary
password=secret
email=something

我想创建一个shell脚本来更改

[profile.two]
中的密码。新密码将是一个变量(由另一个 shell 脚本触发)。例如,新密码将显示为
$1

我知道我应该使用 sed,但在那之后我很难弄清楚语法。

linux shell sed
1个回答
0
投票
sed -i '/`\[`profile`\.`two`]`/{n;n;s/secret/"$1"/}' fileName

Note: [ and . characters need to be escaped to match literal

© www.soinside.com 2019 - 2024. All rights reserved.