473,769 Members | 5,374 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

sed command fails if line has quote

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
10 5251
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
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
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
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,$str 1_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
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_conten ts, then do
str_replace('<m eta http-equiv="Content-Type" content="text/html;
charset=<?php echo CHARSET; ?>">', '', $contents);
Jul 17 '05 #6
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=<?p hp 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_conten ts, then do
str_replace('<m eta 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('<m eta 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
Jack <ve****@twmi.rr .com> wrote:
str_replace('<m eta 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_conten ts()
instead.

--

Daniel Tryba

Jul 17 '05 #8
Daniel Tryba wrote:
Jack <ve****@twmi.rr .com> wrote:
str_replace('<m eta http-equiv="Content-Type" content="text/html;
charset=<?p hp 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_conten ts()
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\=\"Conten t-Type\"
content\=\"text \/html\; charset\=\<\?ph p echo CHARSET\; \?\>\"\>', " ",
$line))) {

Jack
Jul 17 '05 #9
Jack <ve****@twmi.rr .com> wrote in message news:<FZ******* ************@fe 2.columbus.rr.c om>...
Daniel Tryba wrote:
Jack <ve****@twmi.rr .com> wrote:
str_replace('<m eta http-equiv="Content-Type" content="text/html;
charset=<?p hp 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_conten ts()
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\=\"Conten t-Type\"
content\=\"text \/html\; charset\=\<\?ph p echo CHARSET\; \?\>\"\>', " ",
$line))) {

Jack


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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
9897
by: Kartik | last post by:
Hi, I have an ASP.NET application using VB.NET.I am sending a DOS command to a machine on the network to print a file. This is achieved using xp_cmdshell Dim str As String = "xp_cmdshell ""type " & nwkfilepath & " > " & pPrinterName & " """
7
4737
by: Steve M | last post by:
I'm trying to invoke a Java command-line program from my Python program on Windows XP. I cannot get the paths in one of the arguments to work right. The instructions for the program describe the following for the command-line arguments: java -jar sforcedataloader.jar -Dsalesforce.config.dir=CONFIG_DIRECTORY They also give an example:
6
2935
by: Edd Dawson | last post by:
Hi. I have a strange problem involving the passing of command line arguments to a C program I'm writing. I tried posting this in comp.programming yesterday but someone kindly suggested that I'd have better luck here. So here goes! My program ignores any command line arguments, or at least it's supposed to. However, when I pass any command line arguments to the program, the behaviour of one of the functions changes mysteriously. I have...
7
2578
by: John F | last post by:
Not exactly a C question (sorry), but I couldn't find a ng that seems more suitable... On the Unix shell command line I can successfully write cc -DPATH=\"abcd/\" program.c -o program to compile program as if it contained the line #define PATH "abcd/" But using the free MinGW http://www.mingw.org Windows C compiler, then
3
2155
by: Bob Stearns | last post by:
I am trying to script a manual operation using DB2's command line. Simple commands are working, but the command entered below displays a "db2 (cont.) =>" prompt, then fails on any input? I'm missing something obvious, but I don't see it. P.S. There are no trailing spaces. db2 'load \ from AAA_Auto.TAB of del \ modified by dateformat=\"m/d/yyyy\" coldel0x09 \
13
2693
by: Pieter Edelman | last post by:
Hi, I'm currently writing a command-line program in Python, which takes commands in the form of: ../myprog.py ARGS So pretty standard stuff. In my case, ARGS is a list of image files. One of the possible options is to specify a file holding information about the photos. You'd specify it with (in this particular case) the - t switch, and you can specify multiple files by repeating this switch:
10
13138
by: Justin | last post by:
We have a simple shell script called testsp.sh (located at /home/ userid) Within the shell script, the following command is called: db2 -tvf callsp.sql When the shell script is run in command line mode, it works as expected. and running db2 -tvf ... in command line it also works. The issue occurs when running using "plink"
5
1679
by: qazwart | last post by:
I am reading from a "cvs rlog" command, and I need both the STDOUT and STDERR. Unfortunately, something very strange is happening. If I do this: $cmd = "$cvs_cmd -q rlog -NS -r$from_label::$to_label neo 2>&1"; // Hard coded module DEBUG ("if (!($cvs_fh = popen(\"$cmd\", 'r'))) {"); if (!($cvs_fh = popen("$cmd", 'r'))) { generate_error("Canot open command \"$cmd\" for reading"); }
4
1367
by: Rahul | last post by:
Hello, I am trying to compile a Windows Forms project through command line. I have added some images on the buttons. These images are added as a resource and stored in the "Resource" directory inside the project directory. My project is quite small, (only a single form). I tried running csc in the same folder as my source files using command:
0
9589
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9996
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9865
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6674
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5307
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3964
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3564
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.