sed syntax error - shell

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.

Related

Use a shell script to replace text with pwd

file.txt
...
<LOCAL_PATH_TO_REPO>/src/java/example.java
...
^A longer file but this pretty much explains what I am trying to do.
script.sh
dir=$(pwd)
# replace <LOCAL_PATH_TO_REPO> with dir
I tried using the sed command but it did not work for some reason. Any ideas on how to do this?
Your error means you have backslashes in the variable text.
The simplest solution is to change the delimiter to the one that does not occur in the variable text.
If there are no commas use a comma:
sed -i "s,LOCAL_PATH_TO_REPO,$PWD," file.yml
The -i flag introduces changes into the input file (works for GNU sed).

Changing the prefix of a file with sed

I would like some advice on this script.
I'm trying to use sed (I didn't manage it with rename) to change a file that contains lines of the format (my test file name is sedtest):
COPY W:\Interfaces\Payments\Tameia\Unprocessed\X151008\E*.*
(that's not the only content of the file).
My goal is to replace the 151008 date part with a different date, I've tried to come up with a solution in sed using this:
sed -i -e "s/Unprocessed\X.*/Unprocessed\X'BLABLA'/" sedtest
but it doesnt seem to work, the line remains unchanged, it's like it doesn't recognize the pattern because of the \. I've tried some alternative delimiters like #, but to no avail.
Thanks in advance for any advice.
There's a couple of issues with your sed command. I would suggest changing it to this:
sed -r 's/(Unprocessed\\X)[0-9]+/\1BLABLA/' file
Since your version of sed supports -i without requiring that you add a suffix to create a backup file, I assume you're using the GNU version, which also supports extended regular expressions with the -r switch. The command captures the part within the () and uses it in the replacement \1. Don't forget that backslashes must be escaped.
If you're going to use -i, I would recommend doing so like -i.bak, so a backup of your file is made to file.bak before it is overwritten.
You haven't shown the exact output you were looking for but I assumed that you wanted the line to become:
COPY W:\Interfaces\Payments\Tameia\Unprocessed\XBLABLA\E*.*
Remember that * is greedy, so .* would match everything up to the end of the line. That's why I changed it to [0-9]+, so that only the digits were replaced, leaving the rest of the line intact.
As you've mentioned using a variable in the replacement, you should use something like this:
sed -r -i.bak "s/(Unprocessed\\X)[0-9]+/\1$var/" file
This assumes that $var is safe to use, i.e. doesn't contain characters that will be interpreted by sed, like \, / or &. See this question for details on handling such cases reliably.

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

How to replace a line in sed?

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

Resources