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

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;

Related

How to replace all occurrences using sed

I couldn't make it work the way I want.
echo 'https://aaa/xx_one/bbb/xx_two/ccc' | sed 's|^\(https://.*\)xx_|\1OK_|g'
https://aaa/xx_one/bbb/OK_two/ccc
The output I wanted is: https://aaa/OK_one/bbb/OK_two/ccc
That is, I just want to replace every occurrences of xx_ with OK_ and with that specific constraint.
Need some help. I want it in sed. Thanks.
If the line should start with https://, which you originally didn't say, you can use an "address" in sed:
sed '\=^https://= s/xx_/OK_/g'
Original answer: (will replace xx_ by OK_ after https://, but the line doesn't have to start with it).
Perl to the rescue:
perl -pe '1 while s=https://.*\Kxx_=OK_=g'
-p reads the input line by line, prints each line after processing;
\K in a regular expression forgets what's been matched so far. In this case, it will only replace the xx_ by OK_, but it still has to match https://.* before it;
The substitution runs in a while loop until there's nothing to replace.

Delete line from file if matches regular expression

I am attempting to delete a line from a text file if it matches a regular expression. To accomplish this I was using sed in an Ubuntu environment combined with regular expressions. I have tried/referenced the following solutions: Sol1, Sol2, Sol3.
My current command is: sed '/[^"]+},/d' test.json with this command I am attempting to match and remove lines like:
{"hello},
{"go penguins!},
{"someone help1),
I am NOT trying to match or remove lines like: "should not match regex"}, Any line that ends with "}, should not be deleted.
I am not tied to using sed so any acceptable answer would work so long as my text file would look something like:
...
{"omg this is amazing"},
{"thanks for your help"},
{"no problem"},
...
How about sed '/\"},/!d' test.json?
It should by sed '/\"},/d' test.json (without !)

Use sed to replace ony one occurrence in a certain block

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

Printing all words that start with "#" using sed in BASH

I have a file with a lot of text, but I want to print only words that contain "#" at the beginning. Ex:
My name is #Laura and I live in #London. Name=#Laura. City=#London
How can I print all words that start with #?.I did this the following and it worked, but I want to do it using sed. I tried several patters, but I cannot make it print anything.
grep -o -E "#\w+" file.txt
Thanks
Use this sed command:
sed 's/[^#]*\(#[^ .]*\)/\1\n/g' file.txt
Explanation: we invoke the substitution command of sed. This has following structure: sed 's/regex/replace/options'. We will search for a regex and replace it using the g option. g makes sure the match is made multiple times per line.
We look for a series of non at chars followed by an # and a number of non-spaces #[^ ]*. We put this last part in a group \(\) and sub it during the replacement \1.
Note that we add a newline at the end of each match, you can also get the output on a single line by omitting the \n.

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