Use sed to replace ony one occurrence in a certain block - sed

I want to replace ignore_broadcast_ssid=1 with ignore_broadcast_ssid=0
inside the file /var/run/hostapd-phy0.conf.
This would be my first guess:
sed 's/ignore_broadcast_ssid=1/ignore_broadcast_ssid=0/g' /var/run/hostapd-phy0.conf
But this replaces this option globally, How can I only replace this in one of the sections, starting with bss=wlan0-2 inside the file?
...
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
...

You can first find out the line number of the first text as the starting search index:
grep -n "bss=wlan0-2"
Let assume it is at Line 10. Then apply your sed command at follow:
sed '10s/ignore_broadcast_ssid=1/ignore_broadcast_ssid=0/' /var/run/hostapd-phy0.conf
Make sure you don't have the keyword g at the end of the command as it indicates to replace the matching pattern globally.

sed '/bss=wlan0-2/,/ignore_broadcast_ssid/{s/ignore_broadcast_ssid=1/ignore_broadcast_ssid=0/}' file

Related

Why won't the tab be inserted on the first added line?

I am trying to add multiple lines to a file, all with a leading a tab. The lines should be inserted on the first line after matching a string.
Assume a file with only one line, called "my-file.txt" as follows:
foo
I have tried the following sed command:
sed "/^foo\$/a \tinsert1\n\tinsert2" my-file.txt
This produces the following output:
foo
tinsert1
insert2
Notice how the the tab that should be on the first (inserted) line is omitted. Instead it prints an extra leading 't'.
Why? And how can I change my command to print the tab on the first line, as expected?
With GNU sed:
sed '/^foo$/a \\tinsert1\n\tinsert2' file
<---- single quotes! --->
Produces:
foo
insert1
insert2
From the manual:
a \
text Append text, which has each embedded newline preceded by a backslash.
Since the text to be append itself has to to be preceded by a backslash, it needs to be \\t at the beginning.
PS: If you need to use double quotes around the sed command because you want to inject shell variables, you need to escape the \ which precedes the text to be appended:
ins1="foo"
ins2="bar"
sed "/^foo\$/a \\\t${ins1}\n\t${ins2}" file
sed is for doing s/old/new on individual strings, that is all. Just use awk:
$ awk '{print} $0=="foo"{print "\tinsert1\n\tinsert2"}' file
foo
insert1
insert2
The above will work using any awk in any shell on every UNIX box and is trivial to modify to do anything else you might want to do in future.

How to insert sth with sed just once

I'm trying to substitute the first empty line in my input file with a multiline block, i. e. out of
one
two
three
four
five
six
I want to create
one
two
foo
three
four
five
six
For this I tried this sed script:
sed '/^$/i\
\
foo'
But it inserts at /each/ empty line.
How can I tweak this call to sed so that it inserts just at the first occurrence of an empty line? Is there a way to tell sed that now the rest of the input should just be copied from to the output?
I do not want to switch to awk or other shell tools like read in a loop or similar. I'm just interested in the use of sed for this task.
You can loop and print lines until the end of the file:
sed '/^$/{i\
\
foo
:a;n;ba}' file
I found a way by replacing the i with a s command:
sed '0,/^$/s//\
foo\
/'
But I would prefer a solution using the i command because not everything I could want to do after the search might be easily replaceable with an s.

Insert specific lines from file before first occurrence of pattern using Sed

I want to insert a range of lines from a file, say something like 210,221r before the first occurrence of a pattern in a bunch of other files.
As I am clearly not a GNU sed expert, I cannot figure how to do this.
I tried
sed '0,/pattern/{210,221r file
}' bunch_of_files
But apparently file is read from line 210 to EOF.
Try this:
sed -r 's/(FIND_ME)/PUT_BEFORE\1/' test.text
-r enables extendend regular expressions
the string you are looking for ("FIND_ME") is inside parentheses, which creates a capture group
\1 puts the captured text into the replacement.
About your second question: You can read the replacement from a file like this*:
sed -r 's/(FIND_ME)/`cat REPLACEMENT.TXT`\1/' test.text
If replace special characters inside REPLACEMENT.TXT beforehand with sed you are golden.
*= this depends on your terminal emulator. It works in bash.
In https://stackoverflow.com/a/11246712/4328188 CodeGnome gave some "sed black magic" :
In order to insert text before a pattern, you need to swap the pattern space into the hold space before reading in the file. For example:
sed '/pattern/ {
h
r file
g
N
}' in
However, to read specific lines from file, one may have to use a two-calls solution similar to dummy's answer. I'd enjoy knowing of a one-call solution if it is possible though.

how to replace a line based on variable match using sed

Is it possible to use sed to replace some text based on the matching of a condition at the beginning of the text... For example, for the following file, I only want to replace the word 'guest' to 'unwanted-guest' only for the line that begins with the pattern '
541ce0a0c3b4f843ec000001' which is a variable.
541ce0a0c3b4f843ec000001:x:1000:1000:OpenShift guest:/var/lib/openshift/541ce0a0c3b4f843ec000001:/usr/bin/oo-trap-user
541ce468c3b4f843ec000029:x:1001:1001:OpenShift guest:/var/lib/openshift/541ce468c3b4f843ec000029:/usr/bin/oo-trap-user
Try:
sed '/^541ce0a0c3b4f843ec000001/ s/guest/unwanted-guest/'
We have placed a condition in front of the usual sed substitute command. The condition is:
/^541ce0a0c3b4f843ec000001/
This condition limits sed to considering only lines that start with 541ce0a0c3b4f843ec000001 (The caret ^ means must-be-at-the-beginning-of-a-line). The substitute command is:
s/guest/unwanted-guest/
This replaces the first occurrence of guest on the line with unwanted-guest.
Example
Applying this command to your sample input (placed in a file named file):
$ sed '/^541ce0a0c3b4f843ec000001/ s/guest/unwanted-guest/' file
541ce0a0c3b4f843ec000001:x:1000:1000:OpenShift unwanted-guest:/var/lib/openshift/541ce0a0c3b4f843ec000001:/usr/bin/oo-trap-user
541ce468c3b4f843ec000029:x:1001:1001:OpenShift guest:/var/lib/openshift/541ce468c3b4f843ec000029:/usr/bin/oo-trap-user
Using with a variable
$ id=541ce0a0c3b4f843ec000001
$ sed "/^$id/ s/guest/unwanted-guest/" file
541ce0a0c3b4f843ec000001:x:1000:1000:OpenShift unwanted-guest:/var/lib/openshift/541ce0a0c3b4f843ec000001:/usr/bin/oo-trap-user
541ce468c3b4f843ec000029:x:1001:1001:OpenShift guest:/var/lib/openshift/541ce468c3b4f843ec000029:/usr/bin/oo-trap-user

sed replace a word at a line which begins with a specific pattern using

How can I replace a word at a line which begins with a specific pattern on FreeBSD?
Consider the following file contents:
this is to test
that was for test
I want to replace "test" at the line which begins with "this".
In order to perform a replacement for lines starting with this, say:
$ sed '/^this/ s/test/something/' inputfile
this is to something
that was for test
This would replace the word test with something on lines starting with this.
If you want to replace all instances of test on the matching lines, supply the g option to sed:
sed '/^this/ s/test/something/g' inputfile
To make the changes in-place, use the below command:
sed -i '/^this/ s/test/something/g' inputfile;