Jump to content
Boxerman

grab linux output in php

Recommended Posts

Hey guys,

 

I have written an output like the following:

412,Processor 1 CPU 1 VR Temp,Temperature,N/A,N/A,'OK'

as you can see after the 5th comma an output is 'OK'

 

How would one go about chasing the 5th comma and echoing out the OK part?

 

Hopefully this will make you laugh and be super simple!

 

Thanks ahead of time all!

 

B-Man

Edited by Boxerman

Share this post


Link to post
Share on other sites

Thanks!!

 

So, i managed to get this far..

	echo "<br>";
	echo '<pre>';
		print_r(explode(',', $stream));
	echo '</pre>';
	echo '<br>';

This is the output:

Array
(
    [0] => 1
    [1] => Front Panel Board Ambient Temp
    [2] => Temperature
    [3] => 16.00
    [4] => C
    [5] => 'OK'
2
    [6] => System Board Planar 3.3V
    [7] => Voltage
    [8] => 3.34
    [9] => V
    [10] => 'OK'
3
    [11] => System Board Planar 5V
    [12] => Voltage
    [13] => 5.06
    [14] => V
    [15] => 'OK'
4
    [16] => System Board Planar 12V
    [17] => Voltage
    [18] => 12.10
    [19] => V
    [20] => 'OK'

As you can see this is sort of doing what it needed... The new line numbers for 1 is different to 2,3 and 4..

 

Unformatted output is: 

1,Front Panel Board Ambient Temp,Temperature,16.00,C,'OK'
2,System Board Planar 3.3V,Voltage,3.34,V,'OK'
3,System Board Planar 5V,Voltage,5.06,V,'OK'
4,System Board Planar 12V,Voltage,12.10,V,'OK'

SO my question is, how do i get it to spilt each line as a new one? like the unformatted one, the aim is to check the end "OK" and if it is, ignore it but if it is anything that is not "OK" then display on the page..

 

I'm confusing me a little, any help would be greatly appreciated.  

Share this post


Link to post
Share on other sites

OK, so what you told us in the first post was just a small part of the whole story.

 

Is that "unformatted output" held in a text file?

Share this post


Link to post
Share on other sites

the answer is still to use explode (unless you are getting this from a file, in which case file() would be the best first step.). you explode first on the line-ending-character (varies depending on where you are getting the data from, can be \r, \n, \r\n, or \n\r), to give an array of individual lines. you then explode each line on the comma character.

 

you would want to trim() the final data before you try to use it in case there's some white-space characters as part of the data.

Edited by mac_gyver

Share this post


Link to post
Share on other sites

Hi All,

 

Thanks for the advise!

 

This is ran via ssh2, and stored into the $stream variable.

 

The end goal would be to scan the output for anything that does not have the "OK" in. If if doesn't have the "OK" then I want it to echo out that entire line.

 

What would you recommend?

Share this post


Link to post
Share on other sites

does this do it?


$data = explode("\n", $stream);
foreach ($data as $line) {
    $arr = str_getcsv($line,',', "'");
    if ($arr[5] != 'OK') {
        echo $line.'<br>';
    }
}
  • Like 1

Share this post


Link to post
Share on other sites

Wow thats exactly what i wanted! 

 

Thank you so much, How would i go about counting total in the array and then comparing to total OK and total NOT OK?

 

for example:

 

Testing 400 sensors, 380 OK and 20 NOT OK.

 

20 not ok entires listed.

 

Super sorry, and thank you, for taking the time to help a man in need!

Share this post


Link to post
Share on other sites

like this

$data = explode("\n", $stream);
$fail=0;
foreach ($data as $line) {
    $arr = str_getcsv($line,',', "'");
    if ($arr[5] != 'OK') {
        echo $line.'<br>';
        ++$fail;
    }
}
$tot = count($data);
printf("<hr>%d sensors tested<br>%d OK<br>%d failed", $tot, $tot-$fail, $fail);
  • Like 1

Share this post


Link to post
Share on other sites

Ah i see  how you're doing it, how would you move that above the output?

 

trying to move $line is not working (i presume its outside the IF so the variable doesn't work)? 

Share this post


Link to post
Share on other sites

It counts them while it is processing and outputting them. If you want the list afterwards then it needs to be stored then printed after the counts are printed.

$data = explode("\n", $stream);
$fail=0;
$failed = '';
foreach ($data as $line) {
    $arr = str_getcsv($line,',', "'");
    if ($arr[5] != 'OK') {
        $failed .= $line.'<br>';  // store failures
        ++$fail;
    }
}
$tot = count($data);
printf("%d sensors tested<br>%d OK<br>%d failed (listed below)<hr>%s", $tot, $tot-$fail, $fail, $failed);

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.