gpt4 book ai didi

regex - sed 匹配模式与变量中的多行字符串

转载 作者:行者123 更新时间:2023-12-04 19:13:49 25 4
gpt4 key购买 nike

我阅读了很多答案和示例,但找不到匹配的示例,因为我也找不到 sed。这是我正在尝试做的事情:

sftp_configuration_lines="Match User ${sftp_username}
ForceCommand internal-sftp
PasswordAuthentication yes
ChrootDirectory /home/${sftp_username}
PermitTunnel no
AllowAgentForwarding no
AllowTcpForwarding no
X11Forwarding no

"

sed -i -e "s|${sftp_configuration_lines_sed}|#removed|g" /etc/ssh/sshd_config;

当然,这个不起作用并给出未终止的命令错误。

我已经在行尾用\尝试过,也用\n 尝试过,但仍然没有运气。我在这里想念什么?

最佳答案

而不是 sed , 你可以使用 awk有一个空的 RS :

sftp_configuration_lines="Match User ${sftp_username}
ForceCommand internal-sftp
PasswordAuthentication yes
ChrootDirectory /home/${sftp_username}
PermitTunnel no
AllowAgentForwarding no
AllowTcpForwarding no
X11Forwarding no"

awk -v RS= -v s="$sftp_configuration_lines" '{gsub(s, "#removed")} 1' /etc/ssh/sshd_config

或者 , 你也可以使用 perl :
perl -0777 -pe "s~$sftp_configuration_lines~#removed~" /etc/ssh/sshd_config

关于regex - sed 匹配模式与变量中的多行字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45616870/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com