how to get line from file, like grep

This is a discussion on how to get line from file, like grep within the PHP forums in Programming Languages category; Hi, I tried searching myself using google and on php.net, but too much irrelevant answers and not a single one helpful so please bare with me. Could someone please show me how to get one line from a file, much like unix "grep '^needle' haystack" ? I'm sure there are zero or one matches, and I don't need fancy regular expressions, just a string to be found at the beginning of a line. That string won't be the entire line though. TIA...

Go Back   Application Development Forum > Programming Languages > PHP

Object Mix

Register FAQ Calendar Search Today's Posts Mark Forums Read
  #1  
Old 08-26-2008, 07:01 PM
mijn naam
Guest
 
Default how to get line from file, like grep

Hi,

I tried searching myself using google and on php.net, but too much
irrelevant answers and not a single one helpful so please bare with me.

Could someone please show me how to get one line from a file, much like unix
"grep '^needle' haystack" ?

I'm sure there are zero or one matches, and I don't need fancy regular
expressions, just a string to be found at the beginning of a line. That
string won't be the entire line though.

TIA

Reply With Quote
  #2  
Old 08-26-2008, 07:51 PM
macca
Guest
 
Default Re: how to get line from file, like grep

fgets()

http://uk2.php.net/manual/en/function.fgets.php
Reply With Quote
  #3  
Old 08-26-2008, 09:01 PM
Jerry Stuckle
Guest
 
Default Re: how to get line from file, like grep

mijn naam wrote:
> Hi,
>
> I tried searching myself using google and on php.net, but too much
> irrelevant answers and not a single one helpful so please bare with me.
>
> Could someone please show me how to get one line from a file, much like
> unix "grep '^needle' haystack" ?
>
> I'm sure there are zero or one matches, and I don't need fancy regular
> expressions, just a string to be found at the beginning of a line. That
> string won't be the entire line though.
>
> TIA
>


You'll have to do it like grep does - read the file in and compare. The
easiest would be to read in a line at a time with fgets().
Alternatively, you could read in a block at a time and parse the block
into lines. More work for you but it could be faster.

Of course, a database would make things much easier :-)

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

Reply With Quote
  #4  
Old 08-26-2008, 09:11 PM
Gary L. Burnore
Guest
 
Default Re: how to get line from file, like grep

On Wed, 27 Aug 2008 01:01:10 +0200, "mijn naam"
<whatever@hotmail.invalid> wrote:

>Hi,
>
>I tried searching myself using google and on php.net, but too much
>irrelevant answers and not a single one helpful so please bare with me.
>
>Could someone please show me how to get one line from a file, much like unix
>"grep '^needle' haystack" ?
>
>I'm sure there are zero or one matches, and I don't need fancy regular
>expressions, just a string to be found at the beginning of a line. That
>string won't be the entire line though.




$line = `grep '^needle' haystack`;






--
gburnore at DataBasix dot Com
---------------------------------------------------------------------------
How you look depends on where you go.
---------------------------------------------------------------------------
Gary L. Burnore | ÝÛ³ºÝ³Þ³ºÝ³³Ýۺݳ޳ºÝ³Ý³Þ³ºÝ³ÝÝÛ³
.. | ÝÛ³ºÝ³Þ³ºÝ³³Ýۺݳ޳ºÝ³Ý³Þ³ºÝ³ÝÝÛ³
Official .sig, Accept no substitutes. | ÝÛ³ºÝ³Þ³ºÝ³³Ýۺݳ޳ºÝ³Ý³Þ³ºÝ³ÝÝÛ³
.. | ÝÛ 0 1 7 2 3 / Ý³Þ 3 7 4 9 3 0 Û³
Black Helicopter Repair Services, Ltd.| Official Proof of Purchase
================================================== =========================
Reply With Quote
  #5  
Old 08-27-2008, 03:58 AM
Erwin Moller
Guest
 
Default Re: how to get line from file, like grep


Gary L. Burnore schreef:
> On Wed, 27 Aug 2008 01:01:10 +0200, "mijn naam"
> <whatever@hotmail.invalid> wrote:
>
>> Hi,
>>
>> I tried searching myself using google and on php.net, but too much
>> irrelevant answers and not a single one helpful so please bare with me.
>>
>> Could someone please show me how to get one line from a file, much like unix
>> "grep '^needle' haystack" ?
>>
>> I'm sure there are zero or one matches, and I don't need fancy regular
>> expressions, just a string to be found at the beginning of a line. That
>> string won't be the entire line though.

>
>
>
> $line = `grep '^needle' haystack`;


Regrettably, we are not all on *nix. ;-)

Regards,
Erwin Moller

--
============================
Erwin Moller
Now dropping all postings from googlegroups.
Why? http://improve-usenet.org/
============================
Reply With Quote
  #6  
Old 08-27-2008, 04:34 AM
mijn naam
Guest
 
Default Re: how to get line from file, like grep

"mijn naam" <whatever@hotmail.invalid> schreef in bericht
news:6d5cb$48b48b34$53565551$17128@cache100.multik abel.net...
> Hi,
>
> I tried searching myself using google and on php.net, but too much
> irrelevant answers and not a single one helpful so please bare with me.
>
> Could someone please show me how to get one line from a file, much like
> unix "grep '^needle' haystack" ?
>
> I'm sure there are zero or one matches, and I don't need fancy regular
> expressions, just a string to be found at the beginning of a line. That
> string won't be the entire line though.



Thanks all. As expected there are many solutions. I got one from elsewhere
as well.

Part of the problem (which I did not mention) is that I needed only the rest
of the line.
So, for "xyz:abc" I'm only interested in "abc". The following function does
this. What do you think of it?

The file is:
aaa:xyz
xyz:abc
bbbqr
[...]


function match($what,$where) {
$x=file_get_contents($where);
$a=explode("\n",$x);
$b=preg_grep("/^{$what}:/",$a);
if (count($b)!=1) return FALSE;
$b=implode("",$b);
$b=explode(":",$b);
if (count($b)!=2) return FALSE;
$b=$b{1};
return $b;
}

Reply With Quote
  #7  
Old 08-27-2008, 06:35 AM
sheldonlg
Guest
 
Default Re: how to get line from file, like grep

mijn naam wrote:
> "mijn naam" <whatever@hotmail.invalid> schreef in bericht
> news:6d5cb$48b48b34$53565551$17128@cache100.multik abel.net...
>> Hi,
>>
>> I tried searching myself using google and on php.net, but too much
>> irrelevant answers and not a single one helpful so please bare with me.
>>
>> Could someone please show me how to get one line from a file, much
>> like unix "grep '^needle' haystack" ?
>>
>> I'm sure there are zero or one matches, and I don't need fancy regular
>> expressions, just a string to be found at the beginning of a line.
>> That string won't be the entire line though.

>
>
> Thanks all. As expected there are many solutions. I got one from
> elsewhere as well.
>
> Part of the problem (which I did not mention) is that I needed only the
> rest of the line.
> So, for "xyz:abc" I'm only interested in "abc". The following function
> does this. What do you think of it?
>
> The file is:
> aaa:xyz
> xyz:abc
> bbbqr
> [...]
>
>
> function match($what,$where) {
> $x=file_get_contents($where);
> $a=explode("\n",$x);
> $b=preg_grep("/^{$what}:/",$a);
> if (count($b)!=1) return FALSE;
> $b=implode("",$b);
> $b=explode(":",$b);
> if (count($b)!=2) return FALSE;
> $b=$b{1};
> return $b;
> }


I think this is MUCH better suited to a database with a call like

$sql = "select the_column from table_foo where the_key like 'what'";

and where the_key is your aaa, bbb, etc. and the_column contains what
you have now on the rest of the line after the ";".

You can easily export your current files into a database table.
Reply With Quote
  #8  
Old 08-27-2008, 07:43 AM
mijn naam
Guest
 
Default Re: how to get line from file, like grep

<sheldonlg> schreef in bericht
news:meidnUPvyoF5sCjVnZ2dnUVZ_j-dnZ2d@giganews.com...

> I think this is MUCH better suited to a database with a call like
>
> $sql = "select the_column from table_foo where the_key like 'what'";
>
> and where the_key is your aaa, bbb, etc. and the_column contains what you
> have now on the rest of the line after the ";".
>
> You can easily export your current files into a database table.



If that php script would be the only user of the file, you would probably
have a good point. Unfortunately that's not the case.

Thanks anyway!

Reply With Quote
  #9  
Old 08-27-2008, 02:45 PM
George Maicovschi
Guest
 
Default Re: how to get line from file, like grep

On Aug 27, 2:43 pm, "mijn naam" <whate...@hotmail.invalid> wrote:
> <sheldonlg> schreef in berichtnews:meidnUPvyoF5sCjVnZ2dnUVZ_j-dnZ2d@giganews.com...
>
> > I think this is MUCH better suited to a database with a call like

>
> > $sql = "select the_column from table_foo where the_key like 'what'";

>
> > and where the_key is your aaa, bbb, etc. and the_column contains what you
> > have now on the rest of the line after the ";".

>
> > You can easily export your current files into a database table.

>
> If that php script would be the only user of the file, you would probably
> have a good point. Unfortunately that's not the case.
>
> Thanks anyway!


Or you could use something like this.

function find_thing($what,$where)
{
$temp=file_get_contents($where);
if (!$start=strpos($temp,$what))
return false;
else
$start+=strlen($what)+1; //Add +1 because of the :
$end=strpos($temp,"\n",$start);
return substr($temp,$start,$end);
}
Reply With Quote
  #10  
Old 08-27-2008, 03:33 PM
mijn naam
Guest
 
Default Re: how to get line from file, like grep

"George Maicovschi" <georgemaicovschi@gmail.com> schreef in bericht
news:2a56182a-4b0e-43f4-b671-2166989d85b1@d45g2000hsc.googlegroups.com...

> Or you could use something like this.
>
> function find_thing($what,$where)
> {
> $temp=file_get_contents($where);
> if (!$start=strpos($temp,$what))
> return false;
> else
> $start+=strlen($what)+1; //Add +1 because of the :
> $end=strpos($temp,"\n",$start);
> return substr($temp,$start,$end);
> }



Looks good, thanks. It will need to be adjusted slightly, else "abc" would
also match "abcdqrs" and return "qrs". It seems that appending ":" to
"$what" is enough.

Do you agree that using "file_get_contents" is more efficient than "while
not found and not eof do fgets ..." ?

Reply With Quote
Reply


Thread Tools
Display Modes


All times are GMT -5. The time now is 04:25 AM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
vB Ad Management by =RedTyger=

In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.