Register a domain and help support LQ
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 08-04-2009, 03:21 AM   #1
LQ Newbie
Registered: Aug 2009
Posts: 3

Rep: Reputation: 0
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.
Old 08-04-2009, 03:32 AM   #2
LQ Newbie
Registered: Dec 2008
Location: Rochester, NY
Distribution: Gentoo, Fedora/CentOS, Ubuntu
Posts: 2

Rep: Reputation: 0
The single quotes with sed prevent variable expansion. Use double quotes.
Old 08-04-2009, 03:38 AM   #3
LQ Newbie
Registered: Aug 2009
Posts: 3

Original Poster
Rep: Reputation: 0
Originally Posted by tsin View Post
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"

Last edited by tkeyser; 08-04-2009 at 03:39 AM.
Old 08-04-2009, 04:21 AM   #4
Senior Member
Registered: May 2007
Location: Sydney
Distribution: RHEL, CentOS, Debian, OS X
Posts: 1,291

Rep: Reputation: 102Reputation: 102
Lightbulb 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
Old 08-04-2009, 05:30 AM   #5
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian except for netbook still on Slackware 13.37 (and I have to support Ubuntu)
Posts: 8,572
Blog Entries: 30

Rep: Reputation: 1182Reputation: 1182Reputation: 1182Reputation: 1182Reputation: 1182Reputation: 1182Reputation: 1182Reputation: 1182Reputation: 1182
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.


Old 08-20-2009, 07:59 PM   #6
LQ Newbie
Registered: Jul 2003
Posts: 20

Rep: Reputation: 0
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?

Last edited by gqchynaboy; 08-20-2009 at 08:06 PM.
Old 08-20-2009, 08:15 PM   #7
LQ Guru
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 658Reputation: 658Reputation: 658Reputation: 658Reputation: 658Reputation: 658
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.

Last edited by jschiwal; 08-20-2009 at 08:19 PM.


html, sed

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
sed with html document jpgauvin Linux - Newbie 4 12-22-2008 07:54 PM
How to edit a HTML file in linux shaiful Linux - Newbie 11 11-24-2008 01:36 PM
SED: edit ip address n1wil Linux - Software 4 01-08-2008 03:18 PM
sed and html naflan Programming 1 01-10-2006 09:51 PM
can sed edit individual characters/portions of a sentence? TGWDNGHN Linux - Software 10 10-18-2005 05:50 AM

All times are GMT -5. The time now is 04:51 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration