By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,722 Members | 1,174 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,722 IT Pros & Developers. It's quick & easy.

sed command fails if line has quote

P: n/a
I'm trying to make a script that will search a list of files and remove
the following line

<meta http-equiv="Content-Type" content="text/html; charset=<?php echo
CHARSET; ?>">

I'm doing this by generating a sed command and using system to execute
it like so:

$t = sprintf("%s \"s/<meta http-equiv="Content-Type" content="text/html;
charset=<?php echo CHARSET; ?>">%s/g\" %s >%s", $sedPath,$str1_rpl,
$infile, $outfile);
system($t);

The problem is the quote found in the text to be replaced. sed sees it
as part of its command and ignores everything after it. For example, if
I use
<meta http-equiv="Content-Type" content=

for the search string, it works. But if I use
<meta http-equiv="Content-Type" content="

it doesn't work. I have tried escaping the quote (\")but that didn't help.

Does anyone have any suggestions as to what else I could try?
Jul 17 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
Jack <ve****@twmi.rr.com> wrote:
[system/sed]
it doesn't work. I have tried escaping the quote (\")but that didn't help.


You'll have to pass a literal \" to the command:
- '\"'
or
- "\\\""

BTW why call sed instead of using php?

--

Daniel Tryba

Jul 17 '05 #2

P: n/a
Daniel Tryba wrote:
Jack <ve****@twmi.rr.com> wrote:
[system/sed]
it doesn't work. I have tried escaping the quote (\")but that didn't help.

You'll have to pass a literal \" to the command:
- '\"'
or
- "\\\""


Is this what you mean?
$t = sprintf("%s \"s/<meta http-equiv='\"'/%s/g\" %s >%s",
$sedPath,$str1_rpl, $infile, $outfile);

I just tried it but it fails the same way.
BTW why call sed instead of using php?


Is there a built-in function to do a search and replace? If not,
wouldn't I have to build the code myself to do it. The only way I can
think to do that is to open the file, read it into an array and search
the it with str_replace then re-write the file. Isn't that effectively
creating another sed application? Seems like a lot of coding for what I
want to do considering there is an external program that already does
it. But I'm relatively new to using php so if I missing an easier way
please let me know. I'm certainly not making any progress doing it this
way.

Jack
Jul 17 '05 #3

P: n/a
Jack <ve****@twmi.rr.com> wrote:
You'll have to pass a literal \" to the command:
- '\"'
or
- "\\\""

Is this what you mean?
$t = sprintf("%s \"s/<meta http-equiv='\"'/%s/g\" %s >%s",
$sedPath,$str1_rpl, $infile, $outfile);

I just tried it but it fails the same way.


[all below untested, I might have forgotten to escape certain
characters]

You have a regexp:
s/<meta http-equiv="/something/g

The sed command:
sed "s/<meta http-equiv=\"/something/g" infile > outfile
(note that " in the regexp needs to be escaped)

A system() command:
system("sed \"s\/<meta http-equiv=\\\"\/something\/g\" infile > outfile")
(note that ",\ and / needed to be escaped)

Alternatively you could use the more readable combination of "
and ' to construct strings.
BTW why call sed instead of using php?


Is there a built-in function to do a search and replace? If not,
wouldn't I have to build the code myself to do it. The only way I can
think to do that is to open the file, read it into an array and search
the it with str_replace then re-write the file.


That is exactly what you could do (except use regexps instead of
str_replace).
Isn't that effectively creating another sed application? Seems like a
lot of coding for what I want to do considering there is an external
program that already does it.
But than you have a script that depends on external commands and so it
wouldn't necessarily run on all machines (win32 doesn't come with sed
eg.)
But I'm relatively new to using php so if I
missing an easier way please let me know. I'm certainly not making
any progress doing it this way.


:)

--

Daniel Tryba

Jul 17 '05 #4

P: n/a
Daniel Tryba wrote:
Jack <ve****@twmi.rr.com> wrote:
You'll have to pass a literal \" to the command:
- '\"'
or
- "\\\""


Is this what you mean?
$t = sprintf("%s \"s/<meta http-equiv='\"'/%s/g\" %s >%s",
$sedPath,$str1_rpl, $infile, $outfile);

I just tried it but it fails the same way.

[all below untested, I might have forgotten to escape certain
characters]

You have a regexp:
s/<meta http-equiv="/something/g

The sed command:
sed "s/<meta http-equiv=\"/something/g" infile > outfile
(note that " in the regexp needs to be escaped)

A system() command:
system("sed \"s\/<meta http-equiv=\\\"\/something\/g\" infile > outfile")
(note that ",\ and / needed to be escaped)


Thanks for the suggestion but it fails the same way. It apparently is
not possible to do this using sed. I guess I will start over and use
your other suggestion of writing my own code.

Jack

Jul 17 '05 #5

P: n/a
Jack <ve****@twmi.rr.com> wrote in message news:<Hc*********************@fe2.columbus.rr.com> ...
I'm trying to make a script that will search a list of files and remove
the following line

<meta http-equiv="Content-Type" content="text/html; charset=<?php echo
CHARSET; ?>">

I'm doing this by generating a sed command and using system to execute
it like so:

$t = sprintf("%s \"s/<meta http-equiv="Content-Type" content="text/html;
charset=<?php echo CHARSET; ?>">%s/g\" %s >%s", $sedPath,$str1_rpl,
$infile, $outfile);
system($t);

The problem is the quote found in the text to be replaced. sed sees it
as part of its command and ignores everything after it. For example, if
I use
<meta http-equiv="Content-Type" content=

for the search string, it works. But if I use
<meta http-equiv="Content-Type" content="

it doesn't work. I have tried escaping the quote (\")but that didn't help.

Does anyone have any suggestions as to what else I could try?


Get the file into a string with file_get_contents, then do
str_replace('<meta http-equiv="Content-Type" content="text/html;
charset=<?php echo CHARSET; ?>">', '', $contents);
Jul 17 '05 #6

P: n/a
Brad Shinoda wrote:
Jack <ve****@twmi.rr.com> wrote in message news:<Hc*********************@fe2.columbus.rr.com> ...
I'm trying to make a script that will search a list of files and remove
the following line

<meta http-equiv="Content-Type" content="text/html; charset=<?php echo
CHARSET; ?>">

I'm doing this by generating a sed command and using system to execute
it like so:

$t = sprintf("%s \"s/<meta http-equiv="Content-Type" content="text/html;
charset=<?php echo CHARSET; ?>">%s/g\" %s >%s", $sedPath,$str1_rpl,
$infile, $outfile);
system($t);

The problem is the quote found in the text to be replaced. sed sees it
as part of its command and ignores everything after it. For example, if
I use
<meta http-equiv="Content-Type" content=

for the search string, it works. But if I use
<meta http-equiv="Content-Type" content="

it doesn't work. I have tried escaping the quote (\")but that didn't help.

Does anyone have any suggestions as to what else I could try?

Get the file into a string with file_get_contents, then do
str_replace('<meta http-equiv="Content-Type" content="text/html;
charset=<?php echo CHARSET; ?>">', '', $contents);


I've tried two approaches. Both created the output file but the change
did not take effect so the problem is still with my search and replace
code. I first tried the str_replace as suggested.

$data = file($infile);
str_replace('<meta http-equiv="Content-Type" content="text/html;
charset=<?php echo CHARSET; ?>">', '', $data);
$fp = @fopen($outfile, "a") or die("Couldn't open $file for writing!");
$numBytes = @fwrite($fp, $data) or die("Couldn't write values to
file!");
fclose($fp);
I then tried looping through the read in file and using this method.

foreach($data as $line) {
if (!preg_replace("/meta http-equiv=/", " ", $line)) {
echo 'FAILED'.'<br>';
}

Can anyone see my error?

Jack
Jul 17 '05 #7

P: n/a
Jack <ve****@twmi.rr.com> wrote:
str_replace('<meta http-equiv="Content-Type" content="text/html;
charset=<?php echo CHARSET; ?>">', '', $data);
$fp = @fopen($outfile, "a") or die("Couldn't open $file for writing!");
$numBytes = @fwrite($fp, $data) or die("Couldn't write values to
file!");
fclose($fp);


str_replace doesn't operate _on_ $data, it returns a new string using
$data as input. Also $data is thr result of a file() so it's an array
instead of a string. You should probably use file_get_contents()
instead.

--

Daniel Tryba

Jul 17 '05 #8

P: n/a
Daniel Tryba wrote:
Jack <ve****@twmi.rr.com> wrote:
str_replace('<meta http-equiv="Content-Type" content="text/html;
charset=<?php echo CHARSET; ?>">', '', $data);
$fp = @fopen($outfile, "a") or die("Couldn't open $file for writing!");
$numBytes = @fwrite($fp, $data) or die("Couldn't write values to
file!");
fclose($fp);

str_replace doesn't operate _on_ $data, it returns a new string using
$data as input. Also $data is thr result of a file() so it's an array
instead of a string. You should probably use file_get_contents()
instead.

Thanks for pointing those errors out. I changed them but the failure
still occurs. The code works fine. The problem is that the string is
causing an escape to be found that should not be there. I'm sure that
it is a matter of not setting up the string properly but I can't figure
it out. Given this line of code,

if (!($line = preg_replace('<meta http-equiv="Content-Type"
content="text/html; charset=<?php echo CHARSET; ?>">', " ", $line))) {

can someone tell me how to properly escape it? I have tried the
following but it doesn't work:

if (!($line = preg_replace('\<meta http-equiv\=\"Content-Type\"
content\=\"text\/html\; charset\=\<\?php echo CHARSET\; \?\>\"\>', " ",
$line))) {

Jack
Jul 17 '05 #9

P: n/a
Jack <ve****@twmi.rr.com> wrote in message news:<FZ*******************@fe2.columbus.rr.com>.. .
Daniel Tryba wrote:
Jack <ve****@twmi.rr.com> wrote:
str_replace('<meta http-equiv="Content-Type" content="text/html;
charset=<?php echo CHARSET; ?>">', '', $data);
$fp = @fopen($outfile, "a") or die("Couldn't open $file for writing!");
$numBytes = @fwrite($fp, $data) or die("Couldn't write values to
file!");
fclose($fp);

str_replace doesn't operate _on_ $data, it returns a new string using
$data as input. Also $data is thr result of a file() so it's an array
instead of a string. You should probably use file_get_contents()
instead.

Thanks for pointing those errors out. I changed them but the failure
still occurs. The code works fine. The problem is that the string is
causing an escape to be found that should not be there. I'm sure that
it is a matter of not setting up the string properly but I can't figure
it out. Given this line of code,

if (!($line = preg_replace('<meta http-equiv="Content-Type"
content="text/html; charset=<?php echo CHARSET; ?>">', " ", $line))) {

can someone tell me how to properly escape it? I have tried the
following but it doesn't work:

if (!($line = preg_replace('\<meta http-equiv\=\"Content-Type\"
content\=\"text\/html\; charset\=\<\?php echo CHARSET\; \?\>\"\>', " ",
$line))) {

Jack


Escape using quotemeta() (or, AFAIK, preg_quote())
Jul 17 '05 #10

P: n/a
Jack <ve****@twmi.rr.com> wrote:
Given this line of code,

if (!($line = preg_replace('<meta http-equiv="Content-Type"
content="text/html; charset=<?php echo CHARSET; ?>">', " ", $line))) {

can someone tell me how to properly escape it? I have tried the
following but it doesn't work:


There is probably nothing wrong with these lines. You are using foreach
to generate $line?

Observer following code:
<?php
$arr[]=0; $arr[]=1; $arr[]=2;

foreach($arr as $line)
$line=$line+1;
print_r($arr);

foreach($arr as $key=>$line)
$arr[$key]=++$line;
print_r($arr);
?>

foreach places a _copy_ in $line, not a *reference*.

foreach($arr &$line) would be a cool! Doesn't work in PHP4, maybe PHP5?

--

Daniel Tryba

Jul 17 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.