search and replace string using sed - linux

i have a sed command like this for search and replace string inside a file:
sed -i -e 's/`db1`./`db2`./g' result/files1.sql
that is working fine to replace the db1 to db2 inside the file of: result/files1.sql
however when i change it to bash and variable format, it does not work.
sed -i -e "s/`${mydbname}`./`${mydbname2}`./g" "${mypath}"
i get error like:
./mycoolscript: line 241: db1: command not found
./mycoolscript: line 241: db2: command not found
any solution would be great.

If is something you need to replace, you will need to escape by . Here it is
sed -i -e "s/\`${mydbname}\`./\`${mydbname2}\`./g" "${mypath}"

Escape the backtick character
sed -i -e "s/\`${mydbname}\`./\`${mydbname2}\`./g" "${mypath}"
Bash treats the part within backticks as a command and first executes that.

Try this
sed -i -e "s/${mydbname}/${mydbname2}/g" "${mypath}"

There is one more way, of using single quotes for literals & double quotes only around variables/escape sequences.
sed -i -e 's/`'"${mydbname}"'`./`'"${mydbname2}"'`./g' "${mypath}"
Because of single quotes, you will not have to escape the special characters.
The trade-off between escaping special characters vs. using mix of single & double quotes would depend on number of special characters vs. number of variables.
If there are too many characters that would need escaping & less number of variables, I would prefer mix of single & double quotes.

Related

sed command to replace 'root'#'localhost' with 'root'#'%'

I am trying to use sed to replace the expression 'root'#'localhost' with 'root'#'%' with no success. Could someone please help me with the command?
I tried the following:
sed -i ’s#\’root\’#\’localhost\’#\’root\’#\’%\’#g’ xyz.sql
sed: -e expression #1, char 1: unknown command: `?'
sed -i -e ’s/localhost/%/g’ xyz_2616.sql
sed: -e expression #1, char 1: unknown command: `?'
First, make sure you're using a single quote. ’ (a.k.a. ’ or unicode 8217) is not the same as ASCII character 39, '.
Next, you can't escape single quotes inside single quotes. Here's an answer I wrote about that some time ago.
You can, however, put single quotes inside double quotes, or escape them outside your single quoted string. For example, either of the following might work:
sed -e "s/'root'#'localhost'/'root'#'%'/g" file.sql
or
sed -e 's/'\''root'\''#'\''localhost'\''/'\''root'\''#'%'\''/g' file.sql
Alternately, you could substitute just the portion you're interested in, trusting that it doesn't appear elsewhere on the same line:
sed -e '/root.#.localhost/s/localhost/%/' file.sql
The ’ character doesn't look like a single quote (') to me. Make sure that you are using single quotes.
The character you're actually typing is a "right single quote mark". What we refer to as a "single quote" is actually an "apostrophe".
The following should work:
sed -e 's/localhost/%/g' rice_2616.sql
or, your first alternatives but with double quotes to avoid having to escape the embedded single quotes (which I presume are apostrophes):
sed -e "s/'root'#'localhost'/'root’#'%'/g" rice_2616.sql

Replace a string using sed

I want to replace a string ip_ttl="1" with ip_ttl="2" using sed.
I've tried sed -i "s/ip_ttl="1"/ip_ttl="2"/g" - but its not working.
Please help!
Put your sed code inside single quotes, because your code already contains double quotes.
sed -i 's/ip_ttl="1"/ip_ttl="2"/g' file
If you put your code within two double quotes, sed would terminate the program once another double quote was reached. So " before 1 was reached, it would consider as the end and terminates the program.
Update:
If the number always changes then it's better to define the pattern which matches any number.
sed -i 's/ip_ttl="[0-9]\+"/ip_ttl="2"/g' file
If you are using quotation marks in your pattern either escape double quotes in pattern:
sed -i "s/ip_ttl=\"1\"/ip_ttl=\"2\"/g"
or enclose whole pattern in single quotes:
sed -i 's/ip_ttl="1"/ip_ttl="2"/g'
Alternatively you can escape the quotation marks
sed -i "s/ip_ttl=\"1\"/ip_ttl=\"2\"/g" file
Sometimes that's useful because you have both single and double quotes in a string you're selecting for.

How to use sed with a variable that needs to be escaped

I have a file, and I am trying to use bask to replace all the contents of a substring with a path.
I can use the command:
sed -i s/{WORKSPACE}/$MYVARIABLE/g /var/lib/jenkins/jobs/MY-JOB/workspace/config/params.ini
My config/params.ini looks like:
[folders]
folder1 = {WORKSPACE}/subfolder1
folder2 = {WORKSPACE}/subfolder2
however, when $MYVARIABLE is a path, it fails (containing slashes), the sed command fails with:
sed: -e expression #1, char 16: unknown option to `s'
When I run through it manually, I see that the $MYVARIABLE needs to have it's path-slashes escaped. How can I modify my sed command to incorporate an escaped version of $MYVARIABLE?
There's nothing saying you have to use / as your delimiter. sed will use (almost) anything you stick in there. I have a tendency to use |, since that never (rarely?) appears in a path.
sridhar#century:~> export boong=FLEAK
sridhar#century:~> echo $PATH | sed "s|/bin|/$boong|g"
~/FLEAK:/usr/local/FLEAK:/usr/local/sbin:/usr/local/games:/FLEAK:/sbin:/usr/FLEAK:/usr/sbin:/usr/games:/usr/lib/lightdm/lightdm:/home/oracle/app/oracle/product/12.1.0/server_1/FLEAK
sridhar#century:~>
Using double-quotes will allow the shell to do the variable-substitution.
Just escape the $ sign, and use a different delimiter:
sed -i 's;{WORKSPACE};\$MYVARIABLE;g' your_file

replace old-link-url to new-link-url with sed

I'm writing a script in bash that would replace old-link-url to new-link-url
my problem is that sed can't replace the url because of the slashes. If i put just some text it works.
my code
sed -e s/"$old_link"/"$new_link"/g wget2.html > playlist.txt
sed supports any character as separator, so if the pattern you are trying to replace contains /, use a different separator. Most commonly used are # and |
sed 's|foo|bar|g' input
sed 's#foo#bar#g' input
Don't forget to put double quotes if you are using variables in sed substitution. Also, if your variable have / then use a different delimiter for sed. You can use _, %, |, # and many more.
So may be something like this would work -
sed -e "s_"$old_link"_"$new_link"_g" wget2.html > playlist.txt

' in sed appearing in the wrong place

I am trying to set an IP in a file with sed. I am running this command
sed -i 's:$dbserver='':$dbserver='10.0.0.2':' t.conf
but when I look in t.conf the line is
$dbserver=10.0.0.2''
Anyone know why the two single quotes are appearing at the end of the line?
I am running Debian Linux
You need to enclose the second sed argument in double quotes:
sed -i "s:$dbserver='':$dbserver='10.0.0.2':" t.conf
This way $dbserver will be substituted with its value before being passed to sed, and the single quotes won't need escaping.
If you want $dbserver to appear literally in the conf file, preceed the dollar signs with a backslash.

Resources