Using sed to change text containing dots - sed

How can I replace website address in my files by using sed.
For example:
address = www.abc.com
And I want to replace it with mail.whatever.net.

If you want to replace all the occurrences of www.abc.com by mail.whatever.net just use
sed "s/www\.abc\.com/mail.whatever.net/g"

or if you can replace the address line:
sed "s;^address = .*;address = mail.whatever.net;"

Related

How to replace an expression containing dash using sed?

I would like to replace the expression "Park-144201" by "Park144201" in a long text file. How to perform it using `sed?
If you want to apply sed to a file to edit just your expression, you could use
sed -i 's/Park-144201/Park144201/g' your_file.txt

Sed replace specific substring

I have a file that's generated as an output to an SQL query. I need to replace the nulls in the file with blanks, so something like
sed -e"s/null//g" would work.
However there's a valid string of the form 'null/' (with a trailing forward slash) and that should not be replaced. Is there a way to replace only 'null' values while leaving 'null/' intact?
The sed one-liner:
sed 's#null\([^/]\|$\)#\1#g' file
should work for your requirement.
It searches pattern: null and followed by a non-slash char (or EOL),
replace with the followed non-slash char.
Thus, null/ won't be touched.
I think this command should be enough:
sed -e "s/null[^/]//g"

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 search and replace on variable string

I'm trying to search and replace a string in a file on the Mac Terminal using sed. I'm able to search and replace a simple string:
sed -i.bak 's/HOSTS/BOASTS/g' file.txt
But I'm trying it on something a little more complicated, basically the string I want to replace looks something like 'HOSTS:"123.123.123.123, 12345"' - with the 123.123.123.123 being a variable IP so I can't exactly search for that, so I'm trying to use regular expressions, mainly the "." to indicate that I don't know what the IP address will be.
I've tried the following with no luck:
sed -i.bak 's/HOSTS:"., 00000"/HOSTS:"999.999.999.999, 00000"/g' file.txt
You could try the following:
echo "HOSTS:\"123.123.123.123, 12345\"" | sed -e 's/[0-9][0-9][0-9]\.[0-9][0-9][0-9]\.[0-9][0-9][0-9]\.[0-9][0-9][0-9]/999.999.999.999/g'.
each [0-9] will look for a digit, and each \. is the actual symbol, not the "match a character" symbol on sed. This assumes that the IPs will always have this structure. If you're dealing with xxx.xxx.xxx.xxx:xxxx you'll have to edit accordingly.

Sed delimiter options

In my code, I need to replace variable assignments with addresses:
sed -i "s/^variable = .*$/variable = http://myaddress/"
Obviously, this does not work because the forward slashes in the address are recognized in the sed command.
I want to keep the $ at the end of the first expression for replacing anything to the end of the line. I also do not want to escape the dollar sign as such, \$ because it will search for a dollar sign.
Also, I don't want to just escape the forward slashes in the address as there are also variables in some places for the addresses.
I've tried using # instead of / but have to include what I don't want to - the \$.
Are there any alternate delimiters I can use that fit my situation?
The $ is interpreted by your shell. Wrap the whole argument to sed with ' to prevent this.
sed -i 's#^variable = .*$#variable = http://myaddress#'
sed -i "s#^variable = .*$#variable = http://myaddress#" file
should work for you.
Note that the $ in the first expression is not literature "dollar", but a regex expression, means, the end of the line.
sed -i 's#^variable = .*#variable = http://myaddress#'