如何在linux终端上替换多个文件中的一行



我有一个包含多个文件的文件夹,每个文件都包含一个类似的字符串

"tree": "/a/anything-here/b/"

对于每个文件,我需要用字符串替换内部"//"(在这种情况下为"anything-here"(之间的内容

我正在使用sed命令,但没有成功,你能帮我吗?

sed -i 's/"root": a/b" .

您可以使用这个sed:

s='"tree": "/a/anything-here/b/"'
sed -E 's~"(/[^/]*/)[^/]*/~1new-string/~' <<< "$s"
"tree": /a/new-string/b/"

或使用awk:

awk -v str='new-string' 'BEGIN{FS=OFS="/"} {$3 = str} 1' <<< "$s"
"tree": "/a/new-string/b/"

最新更新