0

This question is related to Delete a network profile from wpa_supplicant.conf in Linux (raspbian).

Using the following command, we can see the output as file's content with the matched SSID's network block removed.

cat network_block_eg.conf | sed -n '1 !H;1 h;$ {x;s/[[:space:]]*network={\n[[:space:]]*ssid="example"[^}]*}//g;p;}'

Question is how to get the output just as the matched pattern(in this case network={}).

for example, the command shall output

network={
    ssid="example"
    proto=WPA
    key_mgmt=WPA-PSK
    pairwise=TKIP
    group=TKIP
    psk="not so secure passphrase"
    wpa_ptk_rekey=600
}
Cyrus
  • 84,225
  • 14
  • 89
  • 153
  • sed is completely and utterly the wrong tool for this job. Just look at that bizarre cluster of hieroglyphics you're using and how hard it is now to make this most trivial enhancement to it. It's also non-portable and probably very inefficient. You should be using awk instead. [edit] your question to include the sample input from which you'd like to extract that output and add an `awk` tag so we can help you. – Ed Morton May 31 '18 at 17:07

2 Answers2

0
cat network_block_eg.conf | sed -n '/[[:space:]]*network={/ {:a; /}/! {N; ba;}; /ssid="example"/ p}'

Sed command explanation, -n suppress normal output:

/[[: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
}
Hazzard17
  • 633
  • 7
  • 14
0

sed is for simple substitutions on individual lines, s/old/new/, that is all. For anything else you should be using awk as all those crazy sed constructs to do more than that became obsolete in the mid 1980s when awk was invented.

It's hard to say without concise, testable sample input and expected output but given this sample input (from 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
}

What you seem to be asking for would be as simple as this:

$ 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
}

and the opposite would be to simply negate the test for /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
}

If that's not what you want then edit your question to provide sample input and expected output that clarifies your requirements and we can test against so we can provide a different but still clear and trivial awk solution.

Ed Morton
  • 188,023
  • 17
  • 78
  • 185