SED find / replace code in php script - sed

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.

Related

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.

Print pattern on a string with special character

How to print only string figure with the following line :
\begin{figure}[h!]
I tried :
firstLine='\begin{figure}[h!]'
echo $firstLine | sed -n 's/\\begin{\(.*\)}/\1/p'
but returns :
figure[h!] instead of figure
It seems that issue comes from [] or ! character.
firstLine='\begin{figure}[h!]'
echo "$firstLine" | sed 's/.*{\(.*\)}.*/\1/'
Output:
figure
With your code (add .*):
echo $firstLine | sed -n 's/\\begin{\(.*\)}.*/\1/p'
This might work for you (GNU sed):
sed 's/.*{\(.*\)}.*/\1/' file
This assumes there is only one {...} expression and one line.
A more rigorous solution would be:
sed -n 's/.*\\begin{\([^}]*\)}.*/\1/p' file
However nothing would be output if no match was found.

replace a line that contains a string with special characters

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/"

sed property substitution

I've got a property file where I want to do a property substitution, so I wrote a sed patter to change
host = 1234
with another value, but when I execute
echo "host = 1234" | sed 's/\#*\(host[ \t]*\)\=\([ \t]\d*\)/\1\=\1/g'
I got that the substitution is done (host =host) but the \2 atom is also appended to the end of the string (1234). How can I remove it?
`host =host 1234
The first problem is that \d doesn't do what you think. Use [0-9] at least.
You still get host =host out, which seems crazy to me.
EDIT:
Okay
echo "host = 1234" | sed 's/#*\host[ \t]*=[ \t]*\([0-9]*\)/host = asdf/g'
Why capture 'host' if it's always the same? Just rewrite it.
Why preserve the exact tab/space information? Just rewrite it.
Why escape things which are not special?
I hope you get the idea.
But here's what you probably want:
sed '/^#/!s/[ \t]*\([^ \t]*\)[ \t]*=[ \t]*\([^ \t]*\)/\1 = newvalue/g' input_file
This will change anything = anything to anything = newvalue in non-commented lines of input_file. To make it a specific key which is replaced by newvalue, use:
sed '/^#/!s/[ \t]*\(host\)[ \t]*=[ \t]*\([^ \t]*\)/\1 = newvalue/g' input_file
to e.g. replace only lines reading host = anything.
Does this suit your needs?
echo "host = 1234" | cut -d"=" -f 1
yields
host
Then,
echo "host = 1234" | cut -d"=" -f 1
yields
1234