LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices



Reply
 
Search this Thread
Old 08-04-2009, 03:21 AM   #1
tkeyser
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
tsin
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
tkeyser
LQ Newbie
 
Registered: Aug 2009
Posts: 3

Original Poster
Rep: Reputation: 0
Quote:
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
vikas027
Senior Member
 
Registered: May 2007
Location: Sydney
Distribution: RHEL, CentOS, Debian, OS X
Posts: 1,275

Rep: Reputation: 99
Lightbulb You will like this

This might be beneficial for you in future.


Code:
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:


Code:
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:


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


Code:
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:
Code:
sed s/mytext/$var/
I got this info from somewhere
 
Old 08-04-2009, 05:30 AM   #5
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,563
Blog Entries: 29

Rep: Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179
Hello tkeyser

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

The effect of
Code:
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
Code:
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
Code:
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
Code:
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.

Best

Charles
 
Old 08-20-2009, 07:59 PM   #6
gqchynaboy
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
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 655Reputation: 655Reputation: 655Reputation: 655Reputation: 655Reputation: 655
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 www.tldp.org 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.
 
  


Reply

Tags
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 10:52 AM.

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