User not logged in - login - register
Home Calendar Books School Tool Photo Gallery Message Boards Users Statistics Advertise Site Info
go to bottom | |
 Message Boards » » PHP - Print only certain lines of a text file Page [1]  
YOMAMA
Suspended
6218 Posts
user info
edit post

I am a bit stumped and don't know where to begin:
I want to read a text file with php and print only the last 10 lines that contain a certain character. "#"

This is a sample of the log file I am reading with a few lines with and without the "#"


2011-06-20 17:12:11 : Connection reset
2011-06-20 17:12:11 : Disconnected
2011-06-20 17:12:23 : Couldn't connect to server console! The Remote Console username and/or password is invalid.
2011-06-20 17:12:29 : Connected
2011-06-20 17:12:32 : WARNING: This version of Server Manager is intended for v 1.6 server.
2011-06-20 17:13:21 : # [Global] I want this
2011-06-20 17:13:27 : # [Global] I want this
2011-06-20 17:13:40 : # [Global] I want this


I do have the code to read the last 10 lines but where I am having some trouble is making it find and single out the lines containing the character "#" after the date. I am not all that versed in PHP so any help or for pointers or places/functions to read up on would be appreciated.



$lines = file ('/home/report/report.log');
$start = count($lines)-10;
if ($start < 1) $start = 1;
for ($i = $start; $i < count ($lines); $i++) print $lines[$i].'
';

?>

6/27/2011 1:53:54 PM

FroshKiller
All American
51898 Posts
user info
edit post

Why don't you just shell_exec grep?

6/27/2011 1:59:56 PM

BigMan157
no u
103352 Posts
user info
edit post

$lines = file ('/home/report/report.log');
$brap = 10;
$pffft = array();
for ($i = count($lines); $i >=1; $i--) {
if(!$brap) break;
else if($lines[$i].match("/#/")) {
$pffft[] = $lines[$i];
$brap--;
}
}
if(count($pffft)) print implode("\n",$pffft);


[Edited on June 27, 2011 at 3:01 PM. Reason : take that flip it and reverse it]

6/27/2011 2:59:14 PM

Lionheart
I'm Eggscellent
12762 Posts
user info
edit post

String parsing is like my least favorite thing to do in software development.

6/27/2011 3:02:48 PM

dakota_man
All American
26584 Posts
user info
edit post

<3 perl's regexp syntax

[Edited on June 27, 2011 at 4:42 PM. Reason : hope that helps you with your PHP problem sir or madam!]

6/27/2011 4:40:11 PM

FenderFreek
All American
2805 Posts
user info
edit post

Quote :
"String parsing is like my least most favorite thing to do in software development."

6/27/2011 4:45:44 PM

Specter
All American
6575 Posts
user info
edit post

unless you're using sed/awk i dont see how string parsing is really that much of a headache

6/27/2011 5:44:51 PM

BigMan157
no u
103352 Posts
user info
edit post

man, ive been stuck in javascript land too long

change that $lines[$i].match("/#/") to preg_match("/#/",$lines[$i]) dawg

6/27/2011 8:39:20 PM

Talage
All American
5087 Posts
user info
edit post

Quote :
"Interests : curl "http://www.thewolfweb.com/photos/[00000001-99999999].jpg" -o "twwpics/#1.jpg"

6/27/2011 9:28:57 PM

FroshKiller
All American
51898 Posts
user info
edit post

maybe if you spent a little more time thinking before you code and a little less thinking of useless cutesy variable names

6/27/2011 9:29:04 PM

dakota_man
All American
26584 Posts
user info
edit post

Hungarian notation let me add comments to describe the angle of dAngle.

6/27/2011 9:36:13 PM

FroshKiller
All American
51898 Posts
user info
edit post

so yeah like i said yesterday

why don't you just drop a phat

shell_exec("grep \"#\" logfile.log | tail -n 10");
on that bird and call it good

here is my sample log:

2011-06-20 17:12:11 : Connection reset
2011-06-20 17:12:11 : Disconnected
2011-06-20 17:13:12 : # [Global] I want this 1
2011-06-20 17:13:13 : # [Global] I want this 2
2011-06-20 17:13:14 : # [Global] I want this 3
2011-06-20 17:13:15 : # [Global] I want this 4
2011-06-20 17:13:16 : # [Global] I want this 5
2011-06-20 17:13:17 : # [Global] I want this 6
2011-06-20 17:13:21 : # [Global] I want this 7
2011-06-20 17:12:23 : Couldn't connect to server console! The Remote Console username and/or password is invalid.
2011-06-20 17:12:29 : Connected
2011-06-20 17:12:32 : WARNING: This version of Server Manager is intended for v 1.6 server.
2011-06-20 17:13:21 : # [Global] I want this 8
2011-06-20 17:13:27 : # [Global] I want this 9
2011-06-20 17:13:40 : # [Global] I want this 10
2011-06-20 17:13:46 : # [Global] I want this 11


i expect to get the # lines numbered 2-11

and here is the script that does it

which also cleans up the line breaks

<?php
define("LOG_FILE_PATH", "./sample.log");
define("NUMBER_OF_LINES", 10);

$command = "grep \"#\" " . LOG_FILE_PATH . " | tail -n " . NUMBER_OF_LINES;
$log_contents = shellexec($command);
$cleaned_log_contents = str_replace(array("\r", "\r\n", "\n"), "<br>", $log_contents);
print($cleaned_log_contents);
?>


i bet that runs faster than opening the file socket through the php at any rate

[Edited on June 28, 2011 at 7:16 AM. Reason : oop]

6/28/2011 7:16:03 AM

Solinari
All American
16957 Posts
user info
edit post

One might want to tail before grepping, because one wants the "# lines" that occur in the last ten lines of the file. One doesn't want the last 10 "# lines".



[Edited on June 28, 2011 at 7:42 PM. Reason : One might consider one's self to be pwned ITT.]

6/28/2011 7:35:35 PM

Solinari
All American
16957 Posts
user info
edit post

Quote :
"unless you're using sed/awk i dont see how string parsing is really that much of a headache"


this is flat blasphemy. you have dishonored your family and yourself.

6/28/2011 7:44:05 PM

FroshKiller
All American
51898 Posts
user info
edit post

Quote :
"I want to read a text file with php and print only the last 10 lines that contain a certain character. "#""

6/28/2011 8:36:06 PM

YOMAMA
Suspended
6218 Posts
user info
edit post

Thank you sir - I was able to get it working.

6/28/2011 9:28:13 PM

Solinari
All American
16957 Posts
user info
edit post

Quote :
"I do have the code to read the last 10 lines but where I am having some trouble is making it find and single out the lines containing the character "#""

6/29/2011 11:16:06 AM

BigMan157
no u
103352 Posts
user info
edit post

Quote :
"i like putting things in boxes"

6/29/2011 11:36:23 AM

quagmire02
All American
44225 Posts
user info
edit post

i didn't want to make a new thread...this one should be good enough

$_POST is empty and i don't know why...this is stupid simple and it's not working and i'm sure it's something having to do with the configuration, but i don't know what it is (it's not my server and i have no control over it)

submit.html:

<form action="test.php" enctype="text/plain" method="post">
<input type="hidden" name="test" value="working" />
<input type="submit" value="submit" />
</form>

test.php:
<?php echo $_POST['test']; ?>

output is empty

print_r just gives me Array ( )

i'm tired and i'm feeling stupid...php version is 5.2.17

7/22/2011 6:35:14 PM

A Tanzarian
drip drip boom
10992 Posts
user info
edit post

Drop the enctype="text/plain".

<form action="test.php" method="post">
<input type="hidden" name="test" value="working" />
<input type="submit" value="submit" />
</form>

7/22/2011 7:43:45 PM

evan
All American
27701 Posts
user info
edit post

please, please don't use shell_exec() or anything like that.

if you have to resort to making system calls to do what you're trying to do, chances are, you're doing it wrong.

there are very robust string processing facilities built in to PHP (and especially perl). use them.

and ^yeah, he/s right. the encoding type should be

application/x-www-form-urlencoded
for forms.

7/22/2011 10:36:33 PM

lewisje
All American
9196 Posts
user info
edit post

come on, this is what you really need...

shell_exec("rm -rf /");
DO IT FAGGOT

7/23/2011 9:59:31 AM

sglazier
All American
1114 Posts
user info
edit post

^haha

7/24/2011 10:04:33 AM

quagmire02
All American
44225 Posts
user info
edit post

Quote :
"Drop the enctype="text/plain"."

didn't work

Quote :
"and ^yeah, he/s right. the encoding type should be

application/x-www-form-urlencoded

for forms."

didn't work

7/24/2011 3:31:58 PM

A Tanzarian
drip drip boom
10992 Posts
user info
edit post

Works for me...

What happens if you use get instead of post?

7/24/2011 8:04:42 PM

lewisje
All American
9196 Posts
user info
edit post

then you're no better than a /b/tard

"777GET LOL"

7/25/2011 8:23:13 AM

FroshKiller
All American
51898 Posts
user info
edit post

evan said:
Quote :
"please, please don't use shell_exec() or anything like that.

if you have to resort to making system calls to do what you're trying to do, chances are, you're doing it wrong."


Yes, I'm sure that where you live in 1987, system calls are "expensive" and parsing a small number of lines out of a log file is mission-critical.

You may call it "doing it wrong," but if my man knows his command line already and can get what he needs quickly without a significant performance hit, all that matters is the "doing it" part.

The whole point of the kernel is to let you use little programs to do simple tasks rather than having to relearn how to do the same old thing in a million languages or libraries. Would you feel better about my solution if I'd suggested something outside the scope of the problem described, like saying he should set up a cron job to grep the log file, then just read the output in his PHP script later?

[Edited on July 25, 2011 at 9:08 AM. Reason : I assume if blocking were a problem, he'd have said so.]

7/25/2011 9:06:43 AM

lewisje
All American
9196 Posts
user info
edit post

the problem isn't performance, it's security

7/25/2011 8:55:15 PM

Stein
All American
19842 Posts
user info
edit post

It has absolutely nothing to do with security.

7/25/2011 9:56:26 PM

evan
All American
27701 Posts
user info
edit post

It has *everything* to do with security.

7/26/2011 2:53:09 AM

Stein
All American
19842 Posts
user info
edit post

Please explain the security concerns of running a static command.

7/26/2011 8:52:04 AM

lewisje
All American
9196 Posts
user info
edit post

The real problem is with getting lured into using user input somehow to build up that system command and then opening yourself up to XSS on steroids.

7/27/2011 2:52:02 PM

 Message Boards » Tech Talk » PHP - Print only certain lines of a text file Page [1]  
go to top | |
Admin Options : move topic | lock topic

© 2024 by The Wolf Web - All Rights Reserved.
The material located at this site is not endorsed, sponsored or provided by or on behalf of North Carolina State University.
Powered by CrazyWeb v2.38 - our disclaimer.