Replace a string using sed - linux

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.

Related

replace a word with string in a file using shell

I have a file in which I have to convert the string "lock allpages" into STRING
" LOCATION 'hdfs://LOCATION/DIRECTORY/TBL;' " . I tried sed but it's throwing some error. can someone suggest the changes that I need to do. I tried below but it's not working. and also can someone suggest how to use sed for replacing multiple strings at once
sed -i 's/lock allpages/LOCATION 'hdfs://LOCATION/DIRECTORY/TBL';'
You need to escape your single quotes and forward slashes in your sed statement. .. That and you're not COMPLETING the sed statement .. You need to close your second argument with a slash -> / (Or whatever delimiter you choose)
sed -i 's/lock allpages/LOCATION \'hdfs:\/\/LOCATION\/DIRECTORY\/TBL;\'/g'
OR if you put the statement in double quotes . There's no need to escape the single quotes
sed -i "s/lock allpages/LOCATION 'hdfs:\/\/LOCATION\/DIRECTORY\/TBL;'/g"
FURTHER -- If you used a different delimiter .. Like a pipe | -- There would be no need for escaping at all...
sed -i "s|lock allpages|LOCATION 'hdfs://LOCATION/DIRECTORY/TBL;'|g"

remove backslash only from quote character using sed

I have string: this is a [\"sample\"] sample\'s.
What would be the correct way to remove backslashes from the double quote, preserving the double quote.
Expected output: this is a ["sample"] sample\'s.
I've tested: sed -i 's/\\\"//g' file.txt which is removing the "
Your command is almost correct. You just forgot to substitute with a double quote:
sed -i 's/\\"/"/g' file.txt
sed's s command substitutes the first part (between / here) with the second part. The g flag repeats the operation throughout the line. Your mistake was that the second part was empty, thus effectively deleting the whole \" string.
BTW, escaping the double quote is not necessary since your sed command is inside single quotes.

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

search and replace string using sed

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.

' 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