此问题与Delete a network profile from wpa_supplicant.conf in Linux (raspbian).
有关使用以下命令,我们可以将输出视为文件的内容,并删除匹配的SSID网络块。
cat network_block_eg.conf | sed -n '1 !H;1 h;$ {x;s/[[:space:]]*network={\n[[:space:]]*ssid="example"[^}]*}//g;p;}'
问题是如何将输出作为匹配模式(在本例中为network = {})。
例如,命令应输出
network={
ssid="example"
proto=WPA
key_mgmt=WPA-PSK
pairwise=TKIP
group=TKIP
psk="not so secure passphrase"
wpa_ptk_rekey=600
}
答案 0 :(得分:0)
cat network_block_eg.conf | sed -n '/[[:space:]]*network={/ {:a; /}/! {N; ba;}; /ssid="example"/ p}'
Sed命令解释,-n
抑制正常输出:
/[[:space:]]*network={/ { # in a line that matches start block pattern
:a # 'a' label to jump to
/}/! { # if pattern space not contains end block pattern
N # read next line and add it to pattern space
ba # jump back to 'a' label
}
/ssid="example"/ p # now if pattern space contains your desired ssid, print it out
}
答案 1 :(得分:0)
sed用于单个行的简单替换,s/old/new/
,全部。对于其他任何事情你应该使用awk,因为所有那些疯狂的sed结构做的事情都比1980年代中期发明awk时已经过时了。
如果没有简洁,可测试的样本输入和预期输出,但很难说这个样本输入(来自https://w1.fi/cgit/hostap/plain/wpa_supplicant/wpa_supplicant.conf):
$ cat file
# Default LLT value for this interface in milliseconds. The value used in case
# no value provided during session setup. Default is 50 msec.
# fst_llt is in 1..4294967 range (due to spec limitation, see 10.32.2.2
# Transitioning between states).
#fst_llt=100
# Example blocks:
# Simple case: WPA-PSK, PSK as an ASCII passphrase, allow all valid ciphers
network={
ssid="simple"
psk="very secret passphrase"
priority=5
}
# Same as previous, but request SSID-specific scanning (for APs that reject
# broadcast SSID)
network={
ssid="second ssid"
scan_ssid=1
psk="very secret passphrase"
priority=2
}
# Only WPA-PSK is used. Any valid cipher combination is accepted.
network={
ssid="example"
proto=WPA
key_mgmt=WPA-PSK
pairwise=CCMP TKIP
group=CCMP TKIP WEP104 WEP40
psk=06b4be19da289f475aa46a33cb793029d4ab3db7a23ee92382eb0106c72ac7bb
priority=2
}
你似乎要求的就是这么简单:
$ awk -v RS= -v ORS='\n\n' '/ssid="example"/' file
# Only WPA-PSK is used. Any valid cipher combination is accepted.
network={
ssid="example"
proto=WPA
key_mgmt=WPA-PSK
pairwise=CCMP TKIP
group=CCMP TKIP WEP104 WEP40
psk=06b4be19da289f475aa46a33cb793029d4ab3db7a23ee92382eb0106c72ac7bb
priority=2
}
,相反的是简单地否定/ssid="example"/
的测试:
$ awk -v RS= -v ORS='\n\n' '!/ssid="example"/' file
# Default LLT value for this interface in milliseconds. The value used in case
# no value provided during session setup. Default is 50 msec.
# fst_llt is in 1..4294967 range (due to spec limitation, see 10.32.2.2
# Transitioning between states).
#fst_llt=100
# Example blocks:
# Simple case: WPA-PSK, PSK as an ASCII passphrase, allow all valid ciphers
network={
ssid="simple"
psk="very secret passphrase"
priority=5
}
# Same as previous, but request SSID-specific scanning (for APs that reject
# broadcast SSID)
network={
ssid="second ssid"
scan_ssid=1
psk="very secret passphrase"
priority=2
}
如果那不是您想要的,那么编辑您的问题以提供样本输入和预期输出,以澄清您的要求,我们可以进行测试,以便我们可以提供一个不同但仍然清晰和简单的awk解决方案。