Forum Moderators: coopster
Let's say I want to run a script from the Windows command prompt that will read the contents of "test.txt" and evaluate each line, looking for "this is a test", and then print out the line number and line value if found...
The Perl would be something to the effect...
#!/usr/bin/perl
use warnings;
use strict;
open(FHANDLE, "<test.txt");
my @lines = <FHANDLE>;
close(FHANDLE);
my $count = 0;
foreach my $line (@lines) {
$count++;
if ($line =~ /this is a test/) {
print "\n$count: $line\n";
}
}
How would I do this in PHP? Thanks in advance!
Jordan
** could also use
if (preg_match('/this is a test/',$line) {
but then again I could have messed up that syntax
I had the general notion but have never asked that so thought it was a good time ;)
Actually that is part of the reason why I'm learning Perl (and I guess PHP now :) ), because I want to make a couple CLI apps that will run somewhat transparently on this WinXP box, my other RedHat box and my brother's GenToo box. Aside from Perl / PHP the only other options were actual binaries in C# using Ximian mono, or Javabeans/JFC/SWING...C# looked good, but the windows port of mono is very(x5) buggy, and Java is...well...Java. ;)
PHP seems to be very powerful from everything I've been reading, I think I'll be able to accomplish exactly what I'm trying to. :)
Thanks again for the example.
Jordan
This always appears on the command line above the script output...
"Content-type: text/html
X-Powered-By: PHP/4.3.3"
This would be a great time-saver in the browser...but I don't really want it on the command line. ;) I tried adding a shebang I found on PHPFreaks: #!/usr/bin/php -q (I assumed the -q was for quiet), but it still prints out that header.
Any idea how to turn that off?
Jordan
in windows do:
php -q test.php
on linux you can do the same *OR* as the first line of the script (before <?) put:
#!/path/to/php -q
then to "chmod a+x test.php"
now you can just do ./test.php
when bash (or every shell that I know of) is asked to execute a text file it will look for #! and if it sees it the rest of the file will be fed to whatever is after the!.
That's why you have:
#!/bin/bash
or
#!/bin/perl
and now
#!/usr/local/bin/php
:)
daisho.