【问题标题】:Using sed to find a string and replace that line but ignore commented out lines使用 sed 查找字符串并替换该行但忽略注释掉的行
【发布时间】:2020-02-27 04:23:44
【问题描述】:

我正在使用 sed 在 300 个不同配置的 Linux 虚拟机上编辑我的 /etc/samba/smb.conf 文件。

我只想搜索未注释的“server string =”行,并将整行替换为“server string = $VARIABLE SAMBA”,它在读取我的服务器列表时从 do-while 循环中获取 VARIABLE。

所以尝试完成 3 件事 1 - 找到 "server string =" 并将该行替换为 Server string = $VARIABLE"

      sed -i '/server string \=/c\server string = '$VARIABLE' SAMBA' /etc/samba/smb.conf

上面的命令有效,但不幸的是,我的很多机器仍然有解释 cmets,其中还包括“server string =”,我注意到未注释的“server string =”行并不总是在第 1 列中,并且通常是间隔或标签在旧文件中,所以我不能只用 ^ 开头。

这是我在堆栈溢出时读取其他线程并将替换行语法 (\c) 与忽略注释行语法 (^#/!/) 组合起来的命令,但它并不满意。 我在另一篇文章中为“魔术替换”添加了 \v,因为我无法弄清楚到底需要逃避什么。 (我认为 ^ 和 = 需要转义)

      sed -i -e '/^#/!s/\vserver string =/c\server string = '$VARIABLE' SAMBA/g' /etc/samba/smb.conf

这条线现在似乎对我没有任何作用...没有替换,没有变量替换。

如果我用 grep 搜索“服务器字符串 =”,我会看到:(不,我不能手动更改所有 308 台机器)

# server string = is the equivalent of the NT Description field
   server string = 145000web SAMBA ---- THIS LINE WAS NOT REPLACED ----

我对此束手无策。

------------------

使用第一条评论提供的链接我得到了这个:

sed '/^#/!s/test/TEST/g' file.txt - This command works as expected ignoring the commented out lines and replacing the text.

不幸的是,尝试在 c\ 中添加以替换整行出错了:

cTEST
# test
# test
cTEST
a cTEST to find cTEST

未注释的 3 行应该只是说 TEST 我尝试在 s/ 之后使用 c\,这会导致所有替换失败。

【问题讨论】:

标签: search sed replace


【解决方案1】:

对于这样的文件:

# server string = is the equivalent of the NT Description field
   server string = 145000web SAMBA1
#server string = is the equivalent of the NT Description field
       server string = 999999web2 SAMBA2
#   server string = another comment
#          server string = more comments
server string = some value SAMBA3
 server string = some value SAMBA4

还有这样的变量

echo $variable
111111www5

这将使用 gnu sed 完成这项工作,但不会保留空白:

sed 's/^[^#]*server string \=.*/server string = '"$variable"' SAMBA/g' file
# server string = is the equivalent of the NT Description field
server string = 111111www5 SAMBA
#server string = is the equivalent of the NT Description field
server string = 111111www5 SAMBA
#   server string = another comment
#          server string = more comments
server string = 111111www5 SAMBA
server string = 111111www5 SAMBA

要保留空白,您可以使用以下内容:

sed -r 's/(^[^#]*)server string \=.*/\1server string = '"$variable"' SAMBA/g' file

甚至

sed -r 's/(^[^#]*server string \=).*/\1'"$variable"' SAMBA/g' file

PS:我故意省略了-i switch。当您对结果满意时,可以添加它。

【讨论】:

  • sed -i -r 's/(^[^#]*)server string \=.*/\1server string = '"$RNODE"' SAMBA/g' /etc/samba/ smb.conf (您的第二个命令)有效,但不保留此行上的空格,但使用 ---- sed -i -r 's/(^[^#]*)workgroup 保留工作组 = 行上的空格\=.*/\1 workgroup = MYCOMPANY SAMBA/g' /etc/samba/smb.conf(我也尝试了第三个命令,但仍然无法为服务器字符串行保留空格)
  • @NE1scott 在某些情况下而不是在所有情况下都会保留空格,这听起来很奇怪。试试这个:sed -r 's/(^[^#]*[ \t]*)server string \=.*/\1server string = '"$variable"' SAMBA/g' file
  • 我在 \1 和服务器字符串之间缺少一个空格。
猜你喜欢
  • 2011-06-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-02
  • 2014-09-05
  • 2017-11-27
  • 2019-06-30
相关资源
最近更新 更多