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 9 5409
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
==================
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/
============================
"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;
}
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.
<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!
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);
}
"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 ..." ?
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;
} This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: John Doe |
last post by:
I realise that this is not really a python question, but python's the
only language I'd be comfortable trying to deal with this.
What I need is to search a drive and find all the AVI format files...
|
by: Aquila Deus |
last post by:
Hi all!
Is there any command-line tool that can add/delete and replace string
and files in .resources?
|
by: David Isaac |
last post by:
What's the standard replacement for the obsolete grep module?
Thanks,
Alan Isaac
|
by: umut.tabak |
last post by:
Dear all,
I am quite new to python. I would like to remove two lines from a file.
The file starts with some comments, the comment sign is $. The file
structure is
$
$
$
$
|
by: jimb20 |
last post by:
ive looked for this and the basic code for inputs but it doesnt have anything about this
example: say my file looks like this
-------------
yes
no
1
14
-------------
| |
by: flebber |
last post by:
I was working at creating a simple program that would read the content
of a playlist file( in this case *.k3b") and write it out . the
compressed "*.k3b" file has two file and the one I was trying...
|
by: HMS Surprise |
last post by:
Greetings,
Could someone point my muddled head at a/the python repository. I know
that one exists but cannot find it again. In particular I am looking
for a standalone search tool that given a...
|
by: charlyrosario |
last post by:
I am using ksh to get one line csv file from the output of df -k. The only information i need is: File System Name and FS Usage Example:
/,50,/usr,45,/var,43 All info in ONE line
...
|
by: ruthyoung |
last post by:
Since our organization accelerates business applications across the distributed enterprise, we require some D2D backup repository to accelerate file backup and recovery.
Please suggest some...
|
by: kronus |
last post by:
Hi everyone,
This is my first time posting to the UNIX form and it might be a strange request, but I believe I have most of the pieces.
Here's the overall goal -- I am trying to find the links...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| | |