how to replace a line based on variable match using sed - 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

Related

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 !)

sed command to replace a value in a file not using find and replace

I have a file with a string log.txt and inside the file i have multiple lines
line 1 text
line2/random/string/version:0.0.30
line 3 randome stuff
http://someurl:8550/
So currently I use sed to find and replace 0.0.30 to a new value like 0.0.31
with
sed -i s/0.0.30/0.0.31/g log.txt
The problem with this is I need to know the previous value.
Is there a way to always remove 0.0.30 from the string in the file and replace it with a new value ?
Maybe a indexof or a substring.
You can use a regex definition to match 0.0.30 and replace it with 0.0.31 as below. The --posix flag is to ensure no GNU dialects are applied and plain BRE (Basic Regular Expressions) library is used. Since \{2\} is a BRE syntax to match 2 occurrences of the digit.
sed -i --posix 's/[[:digit:]]\.[[:digit:]]\.[[:digit:]]\{2\}/0.0.31/' file
See explanation for regex here.

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

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;

How to replace the nth occurrence of a string using sed

Is there any way to replace the nth occurrence of a string in a file using sed?
I'm using sed -i '0,/jack.*/ s//jill/' to replace the first occurrence.
How can i change it so that it replaces the nth occurrence?
My file contents the following lines:
first line
second line
third line
jack=1
fifth line
jack=
seventh line
I don't know the value after jack=, it can be anything or nothing.
I want to replace the 2nd occurrence of jack= and anything that follows it with jill.
First replace all the newlines with a unique character that does not occur anywhere else in your file (e.g. ^) using tr. You need to do this in order to create a single string for sed. Then pass it to sed and tell it to replace the nth occurrence of your string. Finally, pass the output back through tr to recreate the newlines.
For n=2, the command is:
$ tr '\n' '^' < file | sed 's/jack/jill/2' | tr '^' '\n'
first line
second line
third line
jack
fifth line
jill
seventh line
Update:
It can also be done with sed, WITHOUT changing the newlines first, using the following command:
$ sed ':a;N;$!ba;s/jack/jill/2' file
Alternatively, use awk:
$ awk '/jack/{c+=1}{if(c==2){sub("jack","jill",$0)};print}' file
Try this, sed ':a;N;$!ba;s/word1/word2/n' filename
Here, :a;N;$!ba is used to load the entire file into memory, line by line, so that sed can process the whole file in a single pass. The s/word1/word2/N substitution then replaces every Nth occurrence of word1 with word2.