替换特定行之前的字符串的最新出现 [英] replace the latest occurrence of a string before a certain row

查看:87
本文介绍了替换特定行之前的字符串的最新出现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用ignore_broadcast_ssid=1
替换ignore_broadcast_ssid=0 在文件/var/run/hostapd-phy0.conf中.

这就是我如何找出字符串的行号之前:

This is, how I find out the row number of a string before that string:

LINE=$(grep -n "bss=wlan0-2" | cut -d':' -f1)

这是如何替换此行后面的:

sed $LINE's/ignore_broadcast_ssid=0/ignore_broadcast_ssid=1/g' /var/run/hostapd-phy0.conf

但是,由于该块的第一行并不总是bss=wlan0-2,我该如何只替换同一块中包含ssid=temp_wifi的部分中的一个,而不是替换在找到的字符串之前 ?

But since the first line of the block is not always bss=wlan0-2, how can I only replace inside one of the sections containing ssid=temp_wifi inside the same block, but before the found string?

...

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
ignore_broadcast_ssid=0
...

推荐答案

问题尚不清楚(您要解决的实际问题被您的问题所困扰,因为您将注意力集中在如何使用您所采用的方法解决问题上开头),但听起来您只是想在包含ssid = temp_wifi的块中将某些文本替换为其他文本,而这就是这样:

It's not clear from your question (the actual problem you're trying to solve is obscured by your question being focused on how to solve the question using the approach you have started with) but it sounds like you just want to replace some text with some other text in a block containing ssid=temp_wifi and that'd just be this:

$ awk -v RS= -v ORS='\n\n' '/ssid=temp_wifi/{sub(/ignore_broadcast_ssid=0/,"whatever")} 1' file
...

bss=wlan0-2
ctrl_interface=/var/run/hostapd
ap_isolate=1
disassoc_low_ack=1
preamble=1
wmm_enabled=1
whatever
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
...

以上内容使用您问题中的文本块作为输入.

The above uses the block of text from your question as input.

这篇关于替换特定行之前的字符串的最新出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆