sed not working correctly when using variables - shell

source=<!--jta-data-source>jdbc/FCBDataSource</jta-data-source-->
destination=<jta-data-source>jdbc/FCBDataSource</jta-data-source>
sed -i "s/$source/$destination/g" /home/rohan/R2.5LZN/UIReleasedArea/obp.ui.domain/persistence.xml
I am getting error sed: -e expression #1, char 44: unknown option to s

Consider what happens when the substitution occurs. The command becomes:
sed -i 's/<!--jta-data-source>jdbc/FCBDataSource</jta-data-source-->/<jta-data-source>jdbc/FCBDataSource</jta-data-source>/g' some_filename
Then, sed sees s/<!--jta-data-source>jdbc/FCBDataSource</j… and thinks that you want to replace an occurrence of <!--jta-data-source>jdbc with the text FCBDataSource<, and the s command has an illegal j modifier (and other junk).
You need to pick a delimiter character that does not appear in either the pattern or the replacement text. A , will do.

destination='<!--jta-data-source>jdbc/FCBDataSource</jta-data-source-->'
source='<jta-data-source>jdbc/FCBDataSource</jta-data-source>'
sed -i "s,$source,$destination,g"
/home/rohan/R2.5LZN/UIReleasedArea/obp.ui.domain/persistence.xml
The problem was with the variable
Quotes were required

Related

How to add some string before a particular type of words?

I have a file, whose contents are :-
abc"/wiki/A"def
ghi"/wiki/B"jkl
mno"/wiki/C"pqr
And I want to add "https://en.wikipedia.org" before all /wiki/ in the given sentences.
There can be many more these types of string in the file.
Required results are :-
abc"https://en.wikipedia.org/wiki/A"def
ghi"https://en.wikipedia.org/wiki/B"jkl
mno"https://en.wikipedia.org/wiki/C"pqr
I tried some solutions which are :-
How to use sed command to add a string before a pattern string?
Insert text before a certain line using Bash
as per my guess, it is related to "/" character before "wiki". Ignoring "/" it is working perfectly.
provided solution by me is :-
sed 's//wiki//bar/wiki//g' abcdef
but shown errors is
sed: -e expression #1, char 13: unknown option to `s'
Add \ (back slash) before / (slash) to identification different meaning / in sed.
If you running on terminal necessary -e option to executed. This is manual sed.
-e CMD Add CMD to sed commands to be executed
sed -e 's/wiki/https\:\/\/en.wikipedia.org\/wiki/g'
Please try this:-
sed 's/\/wiki/https:\/\/en.wikipedia.org\/wiki/g' filename

Changing a (full pathname) line using sed `s` operation

I'm trying to change #!/usr/bin/python to #!/usr/bin/python2.6
I've tried the following command line:
sed -i -e 's/.#!/usr/bin/python.*/#!/usr/bin/python2.6/' /usr/bin/yum
...which returns the following error:
sed: -e expression #1, char 11: unknown option to `s'
I can't find a good answer anywhere on Google.
Thanks in advance.
You're getting this error because sed interprets s/.#!/usr/b as your first search & replace command, and b isn't a valid flag for the command.
Indeed, the syntax of these commands is s<delimiter><search pattern><delimiter><replace><delimiter><flags>, where the most widely used <delimiter> is /.
You could escape the / in your search pattern and replace string so they aren't interpreted as the delimiter, however since you've got a lot of them I would suggest using another delimiter.
For example, using + as a delimiter your sed command would become sed -i -e 's+.#!/usr/bin/python.*+#!/usr/bin/python2.6+'.
As a side-note, the first . in your command is probably a mistake as the shebang should be written directly at the beginning of the files it appears in.
I assume that your line is first line from your file. With GNU sed:
sed '1s/python$/&2.6/' file
If you want to edit your file "in place" use sed's option -i.

sed replace command

I want to uncomment one of these two statements as per condition:
#export CATALINA_OPTS="-javaagent:$TOMCAT_INSTALL_LOCATION/$APACHE_TOMCAT_VERSION/lib/oi-mule-libs/usage/injector.jar"
#export CATALINA_OPTS="-javaagent:$TOMCAT_INSTALL_LOCATION/$APACHE_TOMCAT_VERSION/lib/agents/cl-proxy-agent.jar"
I used following sed command:
#sed -i 's/#export CATALINA_OPTS="-javaagent:$TOMCAT_INSTALL_LOCATION/$APACHE_TOMCAT_VERSION/lib/oi-mule-libs/usage/injector.jar"/export CATALINA_OPTS="-javaagent:$TOMCAT_INSTALL_LOCATION/$APACHE_TOMCAT_VERSION/lib/oi-mule-libs/usage/injector.jar"/' $TOMCAT_HOME/bin/init-script.sh
I got following error:
sed: -e expression #1, char 85: unknown option to `s'
Why did I get this error?
How could I achieve my purpose by modifying above command?
You should change your delimeters to the sed command. Your delimiter is '/' which also appers in your pattern and that gives an error. Try this:
sed 's:<pattern to be replaced>:<replacement pattern>:g'
Here g is for global, s for substitute. You can use any other delimiter not appearing in your pattern, I have used colon ':' in the example
You need to either escape slash characters with backslashes or use another character as a delimiter for the "s"ubstitute command (as pointed out by sleeping_dragon)
With that in mind:
sed -e s/^#(.*\/injector.jar.*)/\1/ -e s/^#(.*\/cl-proxy-agent.jar.*)/\1/ $my_file
making the regular expression in round brackets as unique as required to identify the exact string you need uncommenting.

Replace the string "//" with "/// " using the shell

I am trying to generate documentation of the Octave API using doxygen. However, doxygen recognizes only comments after /// to be documentation and the comment style in Octave is to use only //. I decided to run a shell command to change this and found this
sed -i 's/old-word/new-word/g' *.txt
from here. When I test the command using
sed -i 's/////// /g' *.txt
or
sed -i 's/"//"/"/// "/g' *.txt
I get the error:
sed: -e expression #1, char 5: unknown option to `s'
or
sed: -e expression #1, char 6: unknown option to `s'
respectively. How do I correctly use sed to replace // with ///?
Alternatively, is a simpler way, such as, for example, opening all the files simuntaniously in some IDE and refactoring.
Try:
sed -i 's+//+///+g' *.txt
It is unlucky to use / as a delimiter if you need to substitute /// for // xD.
Use strrep() (string replace).
I am assuming variable "s" contains your string containing "//". Add the following code in your script:
strrep (s,"//","///");
In this case / acts as escape sequence for / and // .

Find and replace variables containing non-escaped characters with sed

I can use this, to find all instances of "fly" and replace it with "insect" in my file:
sed -i 's/fly/insect/g' ./animals.txt
How can I find a BASH variable and replace it with another BASH variable? E.g.:
name=$(echo "fly")
category=$(echo "insect")
sed -i 's/$name/$category/g' ./animals.txt
Update:
I am using GNU sed version 4.2.1. When I try the solutions below, it reports this error:
sed: -e expression #1, char 73: unknown option to `s'
Update:
I discovered why the error is coming up. $category frequently contains lots of symbols (e.g. "/", "$", "#", "!", brackets, etc.).
ame=$(echo "fly")
category=$(echo "in/sect")
sed -i "s/$name/$category/g" ./animals.txt
The above code will create the same error:
sed: -e expression #1, char 7: unknown option to `s'
Is there a way to let sed complete the replaces, even when it encounters these symbols?
Using double quotes
Use double quotes to make the shell expand variables while keeping whitespace:
sed -i "s/$name/$category/g" ./animals.txt
Note: if you need to put backslashes in your replacement (e.g. for back references), you need double slashes (\& contains the pattern match):
Using single quotes
If you've a lot shell meta-characters, you can do something like this:
sed -i 's/'$pattern'/'$category'/g' ./animals.txt
I discovered why the error is coming up. $category frequently contains lots of symbols (e.g. "/", "$", "#", "!", brackets, etc.).
If the substitution or replacement portion contains characters like / then we can use different sets of sed delimiters. You can use something like - # % , ; : | _ etc. any character that does not happen to occur in your substitution and replacement.
Use double-quotes so the shell will interpolate your variables:
sed -i "s/$name/$category/g" ./animals.txt
Use double quotes:
sed -i "s/$name/$category/g" ./animals.txt

Resources