使用sed替换特定块中的一个引用

使用sed替换特定块中的一个引用,sed,Sed,我想将ignore\u broadcast\u ssid=1替换为ignore\u broadcast\u ssid=0 在文件/var/run/hostapd-phy0.conf中 这是我的第一个猜测: sed 's/ignore_broadcast_ssid=1/ignore_broadcast_ssid=0/g' /var/run/hostapd-phy0.conf 但这会全局替换此选项,如何仅在其中一个部分中替换此选项,从文件中的bss=wlan0-2开始 ... bss=wlan0

我想将
ignore\u broadcast\u ssid=1
替换为
ignore\u broadcast\u ssid=0

在文件
/var/run/hostapd-phy0.conf

这是我的第一个猜测:

sed 's/ignore_broadcast_ssid=1/ignore_broadcast_ssid=0/g' /var/run/hostapd-phy0.conf
但这会全局替换此选项,如何仅在其中一个部分中替换此选项,从文件中的
bss=wlan0-2
开始

...

bss=wlan0-2
ctrl_interface=/var/run/hostapd
ap_isolate=1
disassoc_low_ack=1
preamble=1
wmm_enabled=1
ignore_broadcast_ssid=0
uapsd_advertisement_enabled=1
auth_algs=1
wpa=0
ssid=temp_wifi
bridge=br-client
bssid=a0:f3:c1:d8:b7:7c

interface=client0
ctrl_interface=/var/run/hostapd
ap_isolate=1
disassoc_low_ack=1
preamble=1
wmm_enabled=1
...

您可以首先找到第一个文本的行号作为起始搜索索引:

grep -n "bss=wlan0-2"
假设它在第10行。然后在以下位置应用sed命令:

sed '10s/ignore_broadcast_ssid=1/ignore_broadcast_ssid=0/' /var/run/hostapd-phy0.conf

确保命令末尾没有关键字g,因为它指示全局替换匹配模式。

您可以首先找到第一个文本的行号作为起始搜索索引:

grep -n "bss=wlan0-2"
sed '/bss=wlan0-2/,/ignore_broadcast_ssid/{s/ignore_broadcast_ssid=1/ignore_broadcast_ssid=0/}' file
假设它在第10行。然后在以下位置应用sed命令:

sed '10s/ignore_broadcast_ssid=1/ignore_broadcast_ssid=0/' /var/run/hostapd-phy0.conf

确保命令末尾没有关键字g,因为它表示要全局替换匹配模式。

您的分区在哪里?我添加了更多详细信息您的分区在哪里?我添加了更多详细信息您需要
LINE=$(grep-n“bss=wlan0-2”| cut-d':'-f1)
只获取线条很棒!用于替换特定字符串后面的内容。现在:您只需要
LINE=$(grep-n“bss=wlan0-2”| cut-d':'-f1)
就可以得到行了!用于替换特定字符串后面的内容。现在:这也起作用了,但是你能添加一个解释和在哪里找到更多信息吗?这将
sed
的搜索和替换(
s///
)限制在包含
bss=wlan0-2
忽略广播\u ssid
的行中。这很好地解释了这一点:-特别是关于“地址范围”的部分,但是你能添加一个解释和在哪里找到更多信息吗?这将
sed
的搜索和替换(
s///
)限制在包含
bss=wlan0-2
忽略广播\u ssid
的行中。这很好地解释了这一点:-特别是关于“地址范围”的部分
sed '/bss=wlan0-2/,/ignore_broadcast_ssid/{s/ignore_broadcast_ssid=1/ignore_broadcast_ssid=0/}' file