Julian Parker
asked on
grep equivalent in php
Hi Everyone,
I have to write a php script that searches a file for a string and then extract a field from the string. If I was going to do this in bash I would just use grep and awk and it would be done in less than a minute, however the platform this time is Windows and it has php installed which is helpful as I need to add the information extracted into a mysql database. Using windows powershell is not an option for me.
As an example, I want to do this
in php..
I have to write a php script that searches a file for a string and then extract a field from the string. If I was going to do this in bash I would just use grep and awk and it would be done in less than a minute, however the platform this time is Windows and it has php installed which is helpful as I need to add the information extracted into a mysql database. Using windows powershell is not an option for me.
As an example, I want to do this
customerid=`grep "Company Name" /tmp/customerlist.csv | awk -F, '{print $3}' | head -1`
in php..
Try 'preg_grep'? http://us2.php.net/manual/en/function.preg-grep.php
ASKER
I cant for the life of me see how I can use it, the examples dont make sense...
Googling is coming up with other examples I dont really understand at the moment either. I guess I'm stuck in bash hell (or is it Heaven) at the moment.
Googling is coming up with other examples I dont really understand at the moment either. I guess I'm stuck in bash hell (or is it Heaven) at the moment.
Here another alternative: http://us2.php.net/manual/en/function.preg-match.php All of the PHP 'preg' functions use PCRE (Perl Compatible Regular Expressions) for matching. There are some other string comparison functions listed near the bottom here: http://us2.php.net/manual/en/function.strstr.php
you can run any shell command like this:
<?php
$output = shell_exec('grep "Company Name" /tmp/customerlist.csv | awk -F, '{print $3}' | head -1');
echo "<pre>$output</pre>";
?>
$output will have the value you see in the shell
<?php
$output = shell_exec('grep "Company Name" /tmp/customerlist.csv | awk -F, '{print $3}' | head -1');
echo "<pre>$output</pre>";
?>
$output will have the value you see in the shell
<?php
$output = shell_exec('grep "Company Name" /tmp/customerlist.csv | awk -F, "{print $3}" | head -1');
echo "<pre>$output</pre>";
?>
$output = shell_exec('grep "Company Name" /tmp/customerlist.csv | awk -F, "{print $3}" | head -1');
echo "<pre>$output</pre>";
?>
ASKER
Thanks for the links and I can see what they are doing (well sort of) but I dont quite get how I can do the search where I need to match a known string to the contents of a file.
I am currently googling this thing to death in the hope that something clicks.
thanks for the help.
I am currently googling this thing to death in the hope that something clicks.
thanks for the help.
ASKER
@liveaspankaj
I'm afraid that bash is not available as it's a windows system which is why I need to resolve using php.
If I was running the script on a linux server there would not be a problem as I would just use bash for this exercise.
I'm afraid that bash is not available as it's a windows system which is why I need to resolve using php.
If I was running the script on a linux server there would not be a problem as I would just use bash for this exercise.
ohhh
you mean you want to using php as a linux alternative for grep.
better install cygwin that is like a mini-linux shell in windows a small efficient thing
you mean you want to using php as a linux alternative for grep.
better install cygwin that is like a mini-linux shell in windows a small efficient thing
ASKER
cant install cygwin, it's a production server. I only have php...
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
i am sure php cant do more than that.
or else you can purchase softwares like PowerGrep
or else you can purchase softwares like PowerGrep
ASKER
I tried this
and got this error;
PHP Warning: preg_match(): Delimiter must not be alphanumeric or backslash in C:\cstoolkit\migrate.php on line 141
Warning: preg_match(): Delimiter must not be alphanumeric or backslash in C:\cstoolkit\migrate.php on line 141
I'll see if I can use findstr (didnt know about that one!)
is there an equivalent `head -1` command in windows??
$CSVFileContents = file_get_contents($CSVFile);
preg_match($companyname, htmlentities($CSVFileContents), $MatchedCompanyNames);
and got this error;
PHP Warning: preg_match(): Delimiter must not be alphanumeric or backslash in C:\cstoolkit\migrate.php on line 141
Warning: preg_match(): Delimiter must not be alphanumeric or backslash in C:\cstoolkit\migrate.php on line 141
I'll see if I can use findstr (didnt know about that one!)
is there an equivalent `head -1` command in windows??
ASKER
annoyingly, (and I could be using it wrong) but findstr seems to treat spaces as a new word to find...
Some of the company names have spaces like `ABC PLC` for example so if I do;
findstr "ABC PLC" customernames.csv
It finds all the results for ABC and PLC, I have tried "ABC\ PLC" but it makes no difference.
In grep I would just get `ABC PLC`
Some of the company names have spaces like `ABC PLC` for example so if I do;
findstr "ABC PLC" customernames.csv
It finds all the results for ABC and PLC, I have tried "ABC\ PLC" but it makes no difference.
In grep I would just get `ABC PLC`
ASKER
:-) I was... I needed to use /c:"ABC PLC"
I'll have another go! <grin>
I'll have another go! <grin>
you can do:
> findstr /c:"ABC PLC" *
or you can try a dot for space which means any character. i dont know what is used for space
> findstr /c:"ABC PLC" *
or you can try a dot for space which means any character. i dont know what is used for space
for -R in grep you can add S
> findstr /sc:"ABC PLC" *
> findstr /sc:"ABC PLC" *
ASKER
I shouldnt need recursive, it's just looking in one csv file...
I now to this;
Is there an equivalent of the `head` command in windows? I only want one result returned in the output so I can just explode it and get the details I need.
Cheers
Jools
I now to this;
$myShellCommand = 'cmd.exe /c findstr /c:"'.$companyname.'" '.$CSVFile;
$MatchedCompanyNames = shell_exec($myShellCommand);
Is there an equivalent of the `head` command in windows? I only want one result returned in the output so I can just explode it and get the details I need.
Cheers
Jools
is that command working?
do you need /c twice???
may be you can explode with /n and then merge as many lines you want
do you need /c twice???
may be you can explode with /n and then merge as many lines you want
ok its late so going to sleep
good night :)
good night :)
ASKER
nighty night....
the first /c is for cmd the 2nd is for findstr....
I'll have a look at the /n idea...
the first /c is for cmd the 2nd is for findstr....
I'll have a look at the /n idea...
ASKER
Brilliant, I can now just pull off the first entry in the array and then explode that to get the fields I need.
$myShellCommand = 'cmd.exe /c findstr /c:"'.$companyname.'" '.$CSVFile;
$MatchedCompanyNames = shell_exec($myShellCommand);
// MatchedCompanyNames is now a load of text, exploding by the new line means we can just pull the first entry off the array...
$OneCompanyName = explode("\n", $MatchedCompanyNames);
ASKER
Thanks for the assistance...