--- Gordon Stewart <[EMAIL PROTECTED]> wrote:
> Hi there, Im not sure if this is a PHP command, or a specific server
> command..
>
> but Im using :-
>
> echo `cat $temp >>$output 2>&1`;
>
>
> To copy one file & append it to another (Basically to create an index
> file, & combine multiple files into one master index file.)
>
> Question :-
>
> would anyone know if this can be changed so that it removes blank
> lines - during the copy ?
>
> Or will I need to use the PHP fopen / fwrite etc commands & loop
> through the source files & re-write it removing blanks..
>
> (I've been told the cat command is faster...)
>
> --
> G
Sure. First of all, cat is a Linux command so I'll assume you are working on
that OS. If your script was installed on a Windows server, this would not
work.
The backtick operator in PHP runs a Linux (or Unix or similar) command at the
command line with the permissions of the owner and group used by the webserver
user (often the user "apache" and group "apache"). The backtick operator `...`
is equivalent to the shell_exec() function. Other similar functions are
exec(), passthru(), and system().
*** These functions run commands at the operating system command line and can
do some real harm so take extra precautions to ensure that your variables
contain ONLY what you expect and nothing more. Proper validation of data is
critical. ***
Since we are talking about Unix/Linux commands here, we can use a number of
ways to filter the stream of content. One of them is sed, the streaming editor
which can use regular expressions to process each line:
echo `cat $temp >>$output 2>&1 | sed -e '/^\s*$/d'`;
Note the difference between the single quotes (or tick) (') surrounding the
regular expression and the backtick which surrounds the entire command.
Since you are trying to display the content immediately and not place it in a
variable (which backtick and shell_exec() are good for) then you could use the
passthru() function to achieve the same effect.
The above example will remove blank lines from the output displayed in your
program. They will still exist in the logfile identified by $output. If you
want to remove them there as well, you'd have to rearrange things a bit:
echo `cat $temp | sed -e '/^\s*$/d' >>$output 2>&1`;
In both cases the regular expression processed looks at the beginning (^) and
end of the line ($) and will take a line with zero or more (*) of any
whitespace character (space, tab, vertical tab) and delete (d) the line which
contains only these characters.
This is one of many solutions to a problem like this. The Unix/Linux commands
have a whole series of filters to process streams of text data. There are rich
possibilities here. This book is useful for Linux commands:
http://www.amazon.com/gp/product/0596006284/ref=ase_timetravellitera
They are arranged into chapters by broad function.
James
Community email addresses:
Post message: [email protected]
Subscribe: [EMAIL PROTECTED]
Unsubscribe: [EMAIL PROTECTED]
List owner: [EMAIL PROTECTED]
Shortcut URL to this page:
http://groups.yahoo.com/group/php-list
Yahoo! Groups Links
<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/php-list/
<*> To unsubscribe from this group, send an email to:
[EMAIL PROTECTED]
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/