By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,474 Members | 966 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,474 IT Pros & Developers. It's quick & easy.

how to get line from file, like grep

P: n/a
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

Aug 26 '08 #1
Share this Question
Share on Google+
9 Replies


P: n/a
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.
js*******@attglobal.net
==================

Aug 27 '08 #3

P: n/a

Gary L. Burnore schreef:
On Wed, 27 Aug 2008 01:01:10 +0200, "mijn naam"
<wh******@hotmail.invalidwrote:
>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/
============================
Aug 27 '08 #4

P: n/a
"mijn naam" <wh******@hotmail.invalidschreef in bericht
news:6d***************************@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
bbb:pqr
[...]
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;
}

Aug 27 '08 #5

P: n/a
mijn naam wrote:
"mijn naam" <wh******@hotmail.invalidschreef in bericht
news:6d***************************@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
bbb:pqr
[...]
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.
Aug 27 '08 #6

P: n/a
<sheldonlgschreef in bericht
news:me******************************@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!

Aug 27 '08 #7

P: n/a
On Aug 27, 2:43 pm, "mijn naam" <whate...@hotmail.invalidwrote:
<sheldonlgschreef in berichtnews:me******************************@gigan ews.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);
}
Aug 27 '08 #8

P: n/a
"George Maicovschi" <ge**************@gmail.comschreef in bericht
news:2a**********************************@d45g2000 hsc.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 "abcd:pqrs" and return ":pqrs". 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 ..." ?

Aug 27 '08 #9

P: n/a
mijn naam wrote:
"mijn naam" <wh******@hotmail.invalidschreef in bericht
news:6d***************************@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
bbb:pqr
[...]
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;
}
If you want to grab the file in an array of lines, use the file()
function. This method is a bit clearer:

function match($needle, $file) {
$ret = false;
$lines = file($file);

foreach ( $lines as $line ) {
list($key,$val) = explode(':',$line);
$ret = $key==$needle ? $val : false;
if ( $ret ) break;
}

return $ret;
}
Aug 28 '08 #10

This discussion thread is closed

Replies have been disabled for this discussion.