473,656 Members | 2,776 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Aug 26 '08 #1
9 5419
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*******@attgl obal.net
=============== ===

Aug 27 '08 #3

Gary L. Burnore schreef:
On Wed, 27 Aug 2008 01:01:10 +0200, "mijn naam"
<wh******@hotma il.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
"mijn naam" <wh******@hotma il.invalidschre ef in bericht
news:6d******** *************** ****@cache100.m ultikabel.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,$wh ere) {
$x=file_get_con tents($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
mijn naam wrote:
"mijn naam" <wh******@hotma il.invalidschre ef in bericht
news:6d******** *************** ****@cache100.m ultikabel.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,$wh ere) {
$x=file_get_con tents($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
<sheldonlgschre ef in bericht
news:me******** *************** *******@giganew s.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
On Aug 27, 2:43 pm, "mijn naam" <whate...@hotma il.invalidwrote :
<sheldonlgschre ef in berichtnews: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!
Or you could use something like this.

function find_thing($wha t,$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($te mp,"\n",$start) ;
return substr($temp,$s tart,$end);
}
Aug 27 '08 #8
"George Maicovschi" <ge************ **@gmail.comsch reef in bericht
news:2a******** *************** ***********@d45 g2000hsc.google groups.com...
Or you could use something like this.

function find_thing($wha t,$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($te mp,"\n",$start) ;
return substr($temp,$s tart,$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_conte nts" is more efficient than "while
not found and not eof do fgets ..." ?

Aug 27 '08 #9
mijn naam wrote:
"mijn naam" <wh******@hotma il.invalidschre ef in bericht
news:6d******** *************** ****@cache100.m ultikabel.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,$wh ere) {
$x=file_get_con tents($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(':',$li ne);
$ret = $key==$needle ? $val : false;
if ( $ret ) break;
}

return $ret;
}
Aug 28 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
2440
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 that are NOT listed with the AVI extension. I'm looking over an old drive of mine from an old computer. I know the files were renamed with the wrong extension, but I know that they were originally AVI files. Can python do this for me? Any...
3
1720
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?
3
3285
by: David Isaac | last post by:
What's the standard replacement for the obsolete grep module? Thanks, Alan Isaac
10
1679
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 $ $ $ $
2
1338
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 -------------
9
7381
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 to read was maindata.xml . I cannot however seem to use the gzip module correctly. Have tried the program 2 ways for no success, any ideas would be appreciated. Attempt 1 #!/usr/bin/python
11
1778
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 path searches files for a text string. Thanks,
1
2587
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 I am using this script: #!/bin/ksh cat dftmp | grep "%" | awk '{printf "%d,%s\n", $5, $6}' > dftmp2 cat dftmp2 | gawk '{var=var "," $0} END{print substr(var,2)}' Now problem is the following:
0
1376
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 rock-solid platform for maintaining backups on-line that can self manage large files like Web 2.0 rich media, video surveillance data.
6
8990
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 in a large web site that are linked to files over 2000k. I used the command line to find all files on the server that are larger than 2000k by using the following: find ./ -size +2000c > files_over_2000_bytes.txt
0
8382
marktang
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8297
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8816
Oralloy
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8717
jinu1996
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7311
agi2029
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5629
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4150
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4300
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1930
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.