Link to home
Start Free TrialLog in
Avatar of Julian Parker
Julian ParkerFlag for United Kingdom of Great Britain and Northern Ireland

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
customerid=`grep "Company Name" /tmp/customerlist.csv | awk -F, '{print $3}' | head -1`

Open in new window


in php..
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

Avatar of Julian Parker

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.
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>";
?>
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.
@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.

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
cant install cygwin, it's a production server. I only have php...
ASKER CERTIFIED SOLUTION
Avatar of liveaspankaj
liveaspankaj
Flag of Nepal image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
i am sure php cant do more than that.
or else you can purchase softwares like PowerGrep
I tried this
$CSVFileContents = file_get_contents($CSVFile);
preg_match($companyname, htmlentities($CSVFileContents), $MatchedCompanyNames);

Open in new window


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??
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`

:-) I was... I needed to use /c:"ABC PLC"

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
for -R in grep you can add S

> findstr /sc:"ABC PLC" *
I shouldnt need recursive, it's just looking in one csv file...

I now to this;
$myShellCommand = 'cmd.exe /c findstr /c:"'.$companyname.'" '.$CSVFile;
$MatchedCompanyNames = shell_exec($myShellCommand);

Open in new window


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
ok its late so going to sleep
good night :)
nighty night....

the first /c is for cmd the 2nd is for findstr....

I'll have a look at the /n idea...

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);

Open in new window

Thanks for the assistance...