-   Programming (
-   -   using sed to edit html (

tkeyser 08-04-2009 03:21 AM

using sed to edit html
I'm having a problem getting sed usage correct. Im replace html in about 950 file across the server.

checkit=size\=\"30\" value\=\"\'\.\$\_POST\[\'tcf_email\'\]
echo $checkit returns > size\="30" value\="\'\.$\_POST\[\'tcf_email\'\]
replaceit="size=\"26\" value=\"'.$_POST['tcf_email']"
echo $replaceit returns > size="26" value="'.['tcf_email']

find / -name anyfile.php -print0 | xargs -0 sed -i.bakto3 's%$checkit%$replaceit%g

Process the file but, doesn't replace the text. I expect its something in checkit. Can someone check it for me and help me correct it?

Thanks in advance.

tsin 08-04-2009 03:32 AM

The single quotes with sed prevent variable expansion. Use double quotes.

tkeyser 08-04-2009 03:38 AM


Originally Posted by tsin (Post 3630464)
The single quotes with sed prevent variable expansion. Use double quotes.

Thanks for the tip, didn't realize that.

I double-quoted, but that didn't correct it.. still think something is wrong with $checkit..

find / -name anyfile.php -print0 | xargs -0 sed -i.bakto3 "s%$checkit%$replaceit%g"

vikas027 08-04-2009 04:21 AM

You will like this
This might be beneficial for you in future.


sed "s/mytext/$var/"
Sometimes that does not work. It would depend on the exact value of mytext. So another solution is to turn the single quoted string into two single quoted strings:


sed 's/mytext/'$var'/'
In this case, the 2nd single quoted string is '/'. Just using a backslash would work to that and it saves a character:


sed 's/mytext/'$var\/
There is actually no reason to quote a slash so this should also work with most shells:


sed 's/mytext/'$var/
And there may be no reason to quote anything at all, but again, the actual vakue of mytext determines this.
So this may work:

sed s/mytext/$var/
I got this info from somewhere :study:

catkin 08-04-2009 05:30 AM

Hello tkeyser :)

I don't understand what you want to set $checkit to.

The effect of

checkit=size\=\"30\" value\=\"\'\.\$\_POST\[\'tcf_email\'\]
is to set $checkit to size="30" and to run a command called value="'.$_POST['tcf_email']

If you want to set $checkit to
size="30" value="'.$_POST['tcf_email']
then you can use

checkit='size="30" value="'"'.\$_POST['tcf_email']"
This is the concatenation of two quoted strings: the first, in single quotes is size="30" value=". All characters in it are taken verbatim until the closing single quote. The second, in double quotes, has some characters that have special meaning within double quotes so they re escaped using \.

The best way to see what you have got in $checkit is

echo "'$checkit'"
Here the single quotes show if there are any leading or trailing whitespace characters in the value of $checkit.

If my assumption about what you want to do is correct we now have what you want in $checkit but you are not out of the woods yet. The "." and "[" characters in $checkit are meaningful to sed. These must be escaped if you want them to be taken literally. I think $checkit must be
size="30" value="'\.$_POST\['tcf_email']
but I'm not certain and can't test.

You could check this by running the sed command manually. Once you are confident you have the right string for $checkit then you can set it in the script. Here's how to do it if my guess was correct

checkit='size="30" value="'"'\\.\$_POST\\['tcf_email']"
If this doesn't work please post a few lines of sample HTML including the string that you want to change.



gqchynaboy 08-20-2009 07:59 PM

I have a question with sed too.
Say if I had a variable

read -p "Enter/Paste the URL >> " t

# Input would be something like "http://someplace/somewhere/somehow/"

sed -e 's/images\//"${t}"/' someFile.html

Since 't' contains '/' and not '\/' "/images/" never gets replaced.

How would I go about solving this problem?

jschiwal 08-20-2009 08:15 PM

The backslash was used to escape the forward slash, which you were using to separate the input and output patterns. You can use other characters instead of a forward slash to avoid the backslashes.

You can instead use:
sed -e 's#images/#'"${t}"'#' someFile.html

You still have the same mistake from the previous post in this thread. You have the variable single quoted.
The "${t}" is taken literally, including the double quotes.

Define the t variable manually. Then enter:
set sed -e 's#images/#'"${t}"'# someFile.html

Now look at:
echo "${@}"
to see how the arguments are expanded before being given to the sed command.

Reading the bash info manual or the advanced bash scripting guide from the site might be a good idea. You problems are with bash and not sed.

All times are GMT -5. The time now is 03:13 PM.