replace a line that contains a string with special characters - sed

i want to replace lines which contains a string that has some special characters.
i used \ and \ for escape special characters but nothing changes in file.
i use sed like this:
> sed -i '/pnconfig\[\'dbhost\'\] = \'localhost\'/c\This line is removed.' tco.php
i just want to find lines that contains :
$pnconfig['dbhost'] = 'localhost';
and replace that line with:
$pnconfig['dbhost'] = '1.1.1.1';

Wrap the sed in double quotes as
sed -i "s/\(pnconfig\['dbhost'\] = \)'localhost'/\1'1.1.1.1'/" filename
Test
$ echo "\$pnconfig['dbhost'] = 'localhost';" | sed "s/\(pnconfig\['dbhost'\] = \)'localhost'/\1'1.1.1.1'/"
$pnconfig['dbhost'] = '1.1.1.1';

Use as below:
sed -i.bak '/pnconfig\[\'dbhost\'\] = \'localhost\'/pnconfig\[\'dbhost\'\] = \'1.1.1.1\'/' tco.php
Rather than modifying the file for the first time, create back up and then search for your pattern and then replace it with the other as above in your file tco.php

You don't have to worry about backslashing single quotes by using double quotes for sed.
sed -i.bak "/pnconfig\['dbhost'\] = 'localhost'/s/localhost/1.1.1.1/g" File

Try this one.
sed "/$pnconfig\['dbhost']/s/localhost/1.1.1.1/"

Related

Sed replace strings starts with special characters

I'm trying to replace strings with sed in ; php_value[date.timezone] = Europe/Riga
i tried something like this:
sed -i 's/; php_value[date.timezone] = Europe/\Riga/; php_value[date.timezone] = America/\Sao_Paulo/g' file
Output:
sed: -e expression #1, char 47: extra characters after command
You can use
sed -i 's/; php_value\[date\.timezone] = Europe\/Riga/; php_value[date.timezone] = America\/Sao_Paulo/g' file
See the online demo.
NOTE:
[ and . are special regex metacharacters and need to be escaped to match literal [ and ., hence, \[ and \. in the regex part
/ is a regex delimiter char here, and should also be escaped. To escape /, use \/. Well, if you use another regex delimiter char, you will have no need escaping /, e.g.
sed -i 's,; php_value\[date\.timezone] = Europe/Riga,; php_value[date.timezone] = America/Sao_Paulo,g' file
See the commas as regex delimiters here.

Capturing groups with sed command

I have strings like below
_c_VehCfg1_oCAN00_f276589c_In_Int_buf *pVehCfg1_oCAN00_f276589c_In_IntBuf = (_c_VehCfg1_oCAN00_f276589c_In_Int_buf *)can_Msg_tmp_buffer;
I want replace can_Msg_tmp_buffer with ptr as below
_c_VehCfg1_oCAN00_f276589c_In_Int_buf *pVehCfg1_oCAN00_f276589c_In_IntBuf = (_c_VehCfg1_oCAN00_f276589c_In_Int_buf *)ptr;
I have tried sed as below
echo "_c_VehCfg1_oCAN00_f276589c_In_Int_buf *pVehCfg1_oCAN00_f276589c_In_IntBuf = (_c_VehCfg1_oCAN00_f276589c_In_Int_buf *)can_Msg_tmp_buffer;" | sed 's/\(_C_[[:alnum:]_]*IntBuf = [[:alnum:]_]*\)can_Msg_tmp_buffer/1\ptr/g'
Still I'm not getting expected result instead sed output is same as input.
The problem is I have strings like below also
_c_GW_C4_oCAN00_f276589c_In_Moto_buf *pGW_C4_oCAN00_f276589c_In_MotoBuf = (_c_GW_C4_oCAN00_f276589c_In_Moto_buf *)can_Msg_tmp_buffer;
I only want to replace where type is ending with _Int_buf not _Moto_buf.
It gets extremely convoluted to match individual words with a regex and get a captured group out of it. One way would be to work with known parts of the string which are guaranteed to occur.
For your case, using the strings _In_IntBuf and can_Msg_tmp_buffer; we try to uniquely identify those pattern of lines and do the substitution
sed 's/\(.*\)_In_IntBuf = \(.*\)can_Msg_tmp_buffer;/\1_In_IntBuf = \2ptr;/'
In case you are ok with awk try following.
awk '/_In_IntBuf =/{sub(/can_Msg_tmp_buffer/,"ptr")} 1' Input_file
In case you want to save output into Input_file itself append > temp_file && mv temp_file Input_file in above code.

Need assistance with escapes in my groovy command

I need to replace a version string in a file. My search pattern is regex
and my replacement is a variable.
String search = "\\d+.\\d+.\\d+-.\\d+"
String replace = "1.0.0-${BUILD_ID}"
MyFile = "foo"
sh ("""
sed -i -r "s/($search/$replace/g)" $MyFile
""")
The result I am getting
+ sed -i -r s/(\d+.\d+.\d+-.\d+/1.0.0-25/g) foo
sed: bad option in substitution expression
I found the issue with my code. If I remove parenthesis (), the string replacement works as a charm.

SED find / replace code in php script

I have a large php script that contains the following line
$user = $_REQUEST['user'];
The exact match only appears once in the entire page. I want to change it to
$user = urldecode($_REQUEST['user']);
Can someone advise the best way ?
I'm thinking SED, but everything I've tried has failed to find and replace it.
Any ideas ?
Thanks
Following should help you in same.
sed 's/^$user = $_REQUEST\['"'"'user'"'"'\]\;$/$user = urldecode($_REQUEST\['"'"'user'"'"'\]);/' Input_file
Let's say following is the Input_file(I am assuming here).
cat Input_file
^#^#^#^#00000305^#^#^#^#^#^#430^#430^#^#^#^#^#^#^#^#^#09079989530
$user = $_REQUEST['user'];
tefqfwqfb$user = $_REQUEST['user'];
wvwrjvnwvjn$user = $_REQUEST['user'];fwvwrev
So after running above code following will be the output.
sed 's/^$user = $_REQUEST\['"'"'user'"'"'\]\;$/$user = urldecode($_REQUEST\['"'"'user'"'"'\]);/' Input_file
^#^#^#^#00000305^#^#^#^#^#^#430^#430^#^#^#^#^#^#^#^#^#09079989530
$user = urldecode($_REQUEST['user']);
tefqfwqfb$user = $_REQUEST['user'];
wvwrjvnwvjn$user = $_REQUEST['user'];fwvwrev
sed approach:
sed -E "s/^(\\\$user = )(\\\$_REQUEST\['user'\])/\1urldecode(\2)/" file.php
$ awk 'index($0,"$user = $_REQUEST[\047user\047];") { sub(/= /,"&urldecode("); sub(/;/,")&") } 1' file
$user = urldecode($_REQUEST['user']);
This might work for you (GNU sed):
sed '/^$user = $_REQUEST\['\''user'\''\];$/s/$_[^;]*/urldecode(&)/' file
Match the string and then use substitution to amend part of it.
N.B. '\'' closes the current single quoted sed command, introduces another single quote and then begins the rest of the sed command i.e. it punches a hole through to the shell and then quotes a single quote.

How do I search for a keyword in a file and print out the string following the keyword?

I want to search for keyword "mykey = " in a file and print out the string that is following the keyword.
I cannot do a "grep", because each line is very long. I just want to extract the string following the keyword.
Here's what I came up with. Not first, but works, and without the final grep.
grep 'mykey = ' file | sed 's/.*\(mykey = [A-Za-z]*\).*/\1/'
Assuming the keyword is a single word and a space follows it, like this:
mykey = myCoolValue
grep 'mykey' /your/file/here | sed -r 's/.*mykey = (^[ ]*) .*/\1/g' | grep .
If you have pcregrep at hand, you can issue this command in terminal or in a script to get only desired text after mykey =
$ pcregrep -o '(?<=mykey = ).+' file
The regex uses a positive lookbehind, where -o returns only the matched text, not the whole line.