bash command sed is outputting an unexpected error - bash

I'm trying replace a word in a file using sed.
In the same bash script I use the command :
sed -i "s/${list[$index]}/${phone}/g" $1
And it's working flawlessly on the first function, but the second function I wrote:
sed -i "s/${list[$index]}/${zipcode}/g" $1
Outputs this error:
sed: -e expression #1, char 0: no previous regular expression
I'm really desperate, I'm pretty sure that it's a dumb mistake I'm doing but I can't sort it out

When the first half of a sed substitute command is empty:
sed 's//foo/' <<< bar
It returns this error:
sed: -e expression #1, char 0: no previous regular expression
Therefore, as William Pursell commented, there's a value of the ${list[#]} array that's empty, or maybe $index is out of the array's range.

Related

Substituting nth occurrence of a string in a for loop, in a file, with another string using sed

I am trying to replace the (j+1)th occurrence of "Nb " with Nbu in my file that looks like this:
Nb Nbc blahblahblah
Nb blablablaNbblabla
Cd Nb
and many lines that follow
where j is a variable in a for loop.
What I want to get is something like this. For example, when j=1:
Nb Nbc blahblahblah
Nbu blablablaNbblabla
Cd Nb
and many lines that follow
The code I have now looks something like the following:
for j in $(seq 1 1 4)
do
sed -i ':a;N;$!ba;s/Nb /Nbu/$((j+1))' file
done
However, I get a error message:
sed: -e expression #1, char 21: unknown option to `s'
The problem seems to come from the $((j+1)) because when I changed the code to
sed -i ':a;N;$!ba;s/Nb /Nbu/2' file
I get my desired output for j=1.
What should the syntax be to include the looping j?
The single quotes around the sed script are preventing variable expansion. You are expecting a number, but sed is seeing a '$'. Not a valid flag for s
Break up the quoted section to allow variable expansion
sed -i ':a;N;$!ba;s/Nb /Nbu/'$((j+1)) file
Here is one using GNU awk and gensub:
$ awk -v j=1 -v RS="" '{print gensub(/Nb /,"Nbu",j+1)}' file
Nb Nbc blahblahblah
Nbu blablablaNbblabla
Cd Nb
and many lines that follow
If there are more than one paragraph in the text, awk -v RS="^$" is the way.

Bash script sed

I am trying to use sed in bash script as follows:
#!/bin/bash
for i in `seq 1 10`;
do
j=$(($i-1))
OLD="-option_something something/string1_${j}.txt"
NEW="-option_somehting something/string1_${i}.txt"
sed -e "s/$OLD/$NEW/g" file_to_edit.txt
# sed -e "s/$OLD/$NEW/g" file_to_edit.txt > file_to_edit.txt.tmp && mv file_to_edit.txt.tmp file_to_edit.txt
done
But I keep getting following error:
sed: -e expression #1, char 71: unknown option tos'`
I tried the commented line as well, but it does not work too.
It works fine on command line. I do not know what is the problem in script.
Any suggestions? Thanks.
You have a / in the value of OLD and NEW, which is the same character you're using as the delimiter in your sed expression. So the final expression ends up looking like:
sed -e "s/-option_something something/string1_${j}.txt/-option_somehting something/string1_${i}.txt/g"
Do you see all the / in there? Consider instead:
sed -e "s|$OLD|$NEW|g" file_to_edit.txt
You can use any character as the delimiter for sed's s command.

Replace a variable with text (sed)

I have to find a specific text in the file and then replace that text with some new text.
The contents of the file are:
host=100
servers=4
clients=70
I have tried this:
var=$(grep "servers=" /path/to/file)
sed -i "s/${var}/servers=5/g" /path/to/file
But it gives me the error:
sed: -e expression #1, char 2: unterminated `s' command
Note: All I want is to update the value of each of the variable i.e. servers=4 should be replaced by servers=5.
Please help me figure out the solution.
Thanks.
The output of grep ends with a newline character. sed expects the whole command on one line or escaping line breaks.
However, you can easily achieve the complete task with sed only:
sed -i 's/^servers=[0-9]*$/servers=5/' /path/to/file
sed -i.bak "s/servers=[0-9]*/servers=5/" /path/to/file

Unknown option to 's' in a sed script

I am trying to make a simple replacement for an IPv4 address script.
Here is my code.
#!/bin/sh
sed 's/[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}/192.100.100.100/g/'
What is happening is every time I call:
example.sed example1 > example.output
I get:
sed: -e expression #1, char 75: unknown option to `s'
where the 75th char is the 1 from 192.100.100.100.
Why?
Drop the trailing slash!
sed -e \
's/[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}/192.100.100.100/g'
(The split over two lines attempts to ensure visibility of the end of the string.)
Note that sed starts counting at the s at the start of the s/// string, not at the start of the sed word. The trailing / was at character 75 in that string.
The full script should probably add "all the command line arguments" (aka "$#") too:
sed -e \
's/[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}/192.100.100.100/g' \
"$#"
This will read files if they're specified (and would also accept extra editing commands such as -e 's/.*//'!) or standard input if no arguments are specified. You can put it all on one line in your script; the breaks are for improved clarity on Stack Overflow.

Find and Replace string using sed gives error

I am using shell script. My requirement is to find and replace the string. The string contains "/" char as well. I am getting error sed: -e expression #1, char 18: unterminated `s' command. Can someone tell how should i replace the string which has "/"?
#!/bin/bash
...
search_string="../conf/TestSystem/Inst1.xml"
rep="Inst1/Instrument.xml"
sed -i 's|${line}|${rep}/g' MasterConfiguration.xml
I tried using another sed command but that one also gave error sed: -e expression #1, char 13: unknown option to `s'
sed -e "s/${line}/${rep}/g" MasterConfiguration.xml > tempfile
Whenever you deal with shell-variables you have to get them out of the "sed-string":
For example:
sed -e "s/"${line}"/"${rep}"/g" MasterConfiguration.xml > tempfile
Otherwise sed will treat the chars as-is and search for ${line} literally:
As you see, nothing happens here.
Furthermore, if your variables contain / you need to use another delimiter for sed. I tend to use ~ in such a case, but you're free to use other chars - just be consequent and don't mix them like in your first example-sed-command:
sed 's~'${line}'~'${rep}'/g' //WRONG
sed 's~'${line}'~'${rep}'~g' //RIGHT
Combine both and it will work:
You can try this sed,
sed -i "s#${line}#${rep}#g" MasterConfiguration.xml
Problem:
Instead you have,
sed -i "s|${line}|${rep}/g" MasterConfiguration.xml
It should be,
sed -i "s|${line}|${rep}|g" MasterConfiguration.xml
Syntax:
sed "s|pattern|replacement|g"

Resources