How to replace a line in sed? - bash

I have a configure file which has a line ServerIP= in it. Now I want to use find this line and add a new IP address to it, i.e. replace it with ServerIP=192.168.0.101, what is the command like?

You can employ a find-and-replace command to do this:
sed -e 's/\(^ServerIP=\)/\1192.168.0.101/g' your_file
Are we doing this all over the file or only in one spot? The command above should replace it everywhere. You will have to send the output somewhere. I never edit in place with sed because I make too many mistakes.
One tricky thing is this part, \1192.168.0.101, which actually can be broken down like this:
\1 --> the thing we captured
192.168.0.101 --> the thing we are placing IMMEDIATELY after the thing we captured
Also, you may have other lines that look a little different. But, in the future, look up "sed capture and replace".

This one would work whether there's an existing value in ServerIP or not:
sed -i 's#\([[:blank:]]*ServerIP=\)[[:digit:].]*#\1192.168.0.101#' file
I also suggest that you try to learn using CLI editors like VIM or Nano instead.

try:
sed -i 's/^ *ServerIP=/&192.168.0.101/' file

I would do:
sed -i 's/^ServerIP=$/ServerIP=192.168.0.101/' file.config

Related

Is there a way to add text to the end of a string in a conf file in linux?

I have a problem: I have a file that, if I knew how, I would like to edit from the command. I would like to locate the file by content on that line.
I am in CyberPatriot, and my team is second in my state. I know someone who is on the number one team and I know one of the people on the first team. It kills me so I want to make a list of commands that I can go off of to make it faster and more efficient.
Imagine I had this file:
example
oof
goo
random
yes
and I wanted to change it to this:
example
oof
goo
random 'added text'
yes
How do I do so?
I know I can use the echo command to add text to the end of a file, but I don't know how to add text to the end of a specific line.
Thanks, Owen
You can use sed for this purpose.
sed 's/random/& Hello World/' file
to append text to the matched string.
You can use ^random$ to make sure the entire line is matched, before appending.
If you need to modify the file directly, you can use the -i flag, which facilitates in-place editing. Further, using -i.bak creates a backup of the original file first before modifying it, as in
sed -i.bak 's/random/& Hello World/' file
The original copy of the file can be found in file.bak
More about sed : https://www.gnu.org/software/sed/manual/sed.html
Use something like below
sed '4!d' file | xargs -I{} sed -i "4s/{}/{} \'added text\'/" file
Basically in the above command, we are getting the 4th line of the file using sed sed '4!d' file and then using this line to replace it with the same text and some new text(added text)

Trying to comment out lines in a file with instance of a word

This is probably another super simple question, I just can't figure it out. I'm writing a script that needs to comment out all the lines of a file that have any instance of the word "NETWORKID". The problem is, the code I have is putting a "#" at the beginning of all the lines of the particular file. (/etc/profile)
What I have is this:
grep -i "NETWORKID" /etc/profile | sed 's/^/#/'
Any thoughts? Thanks in advance!
You don't need grep, just used `sed:
sed '/NETWORKID/s/^/#/' /etc/profile
Using -i(something) will update the file and create a backup with the extension, eg
sed -i.bak -e 's/(.*NETWORKID.*)/#\1/' /etc/profile
Will update your profile, and create a copy of how profile was before running the command in /etc/profile.bak

Removing chars until a numeric is found from array item Bash

I have the line of text within a text file. The line looks something like this:
xxxx,xxxxx,xxxxxx,xxxxx,xxxx,NL-1111 xx,xxxx,xxx
The NL- is an identifier for the country so this could be anything. I would like to remove the NL- part from the line so it looks like this:
xxxx,xxxxx,xxxxxx,xxxxx,xxxx,1111 xx,xxxx,xxx
And write the file afterwards.
Thanks in advance.
Another solution close to sed's ones, but with perl:
perl -i -pe "s/(?<=,)[a-zA-Z]{2}-//g" file.txt
It uses look behind expression, so that you don't need to repeat the comma in the replacement part.
something like this using sed
sed -i 's/,[A-Z][A-Z]-\([0-9]\+,\)/,\1/i' file.txt
,[A-Z][A-Z]-\([0-9]\+,\)search for comma letter, letter, -, digit(s), comma
,\1keep only the commas and the digits.
iignore case on the letters
thankyou to #chris for proof-reading.
I think the simplest solution here is reading it from the file into a shell variable, then writing it back immediately and using the pattern substitution variation of parameter expansion:
line="$(<file)"; echo "${line/[a-zA-Z][a-zA-Z]-}" >|file;
I would warn you against solutions that use sed-in-place functionality. I've found that sed behavior differs on different platforms with respect to the -i option. On Mac you have to give an empty argument ('') to the -i option, while on Cygwin you must not have an empty argument following the -i. To get platform compatibility you'd have to test what platform you're on.
sed might do the trick: remove the string ",NL-", "BE-" etc from anywhere in the file:
sed -i 's/,[A-Z][A-Z]-/,/' file.txt

I'm having problems with what I "sed"

I'm trying to replace the port in this config but I'm having issues with it basically ignoring me. What have I done wrong here?
Showing the line I wish to replace.
b#a:/home/mse1# cat server.properties |grep port
server-port=33333
Then showing you the sed I used in several other scripts to do the exact same thing thusly failing to accomplish the task.
b#a:/home/mse1# sed 's/server-port=.*`\n`/server-port=33334/g' /home/mse1/server.properties |grep server-port
server-port=33333
Because you (correctly) used single quotes, the back-quotes are seen by sed simply as a part of the pattern. Also, \n doesn't mean newline in that context — you use $ to look for the end of the line. Hence:
sed 's/^server-port=.*$/server-port=33334/' …
You don't need the g modifier when there's only going to be one match on a line, as there is when you have ^ at the start and $ at the end.
try this below code
sed -i.bak s/server-port=33333/server-port=33334/g /home/mse1/server.properties

sed syntax error

I want to replace a forwarder in my named.conf file through a shellscript. The reasons for this are that I have a firewall, and that have to use my localhost in a VirtualBox VM.
I do this
forwarder="// forwarders {10.123.14.2;10.123.14.3;};"
forwarder1=" forwarders {10.123.14.2;10.123.14.3;};"
sed '/'"$forwarder"'/s/'"$forwarder"'/'"$forwarder1"'/'> /etc/named.conf
But i get a syntax error.
Can anyone tell me what I've done wrong and present a solution?
Within sed the // character is special so you have to escape them.
Try something like this for the forwarder string: forwarder="\/\/...
Sed can use just about any character to delimit the s/search/replace/ strings, try
sed "\#$forwarder#s#$forwarder#$forwarder1#" /etc/named.conf.old > /etc/named.conf
Note that your example did not have an input file and if you really executed above line, you have probably zeroed out your /etc/named.conf
Modern seds support a -i option that will read and save your file In place, i.e.
sed -i "#......" file
I hope this helps.

Resources