LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 12-13-2005, 08:17 AM   #1
ALInux
Member
 
Registered: Nov 2003
Location: Lebanon
Distribution: RHEL 5/CentOS 5/Debian Lenny/(K)Ubuntu Is Dead/Mandriva 10.1
Posts: 668
Blog Entries: 7

Rep: Reputation: 31
PHP + bash: grep, cut or explode ???? :S


Hi guys I want to display the output of
"ps auxc" in a table with only a couple of selected headings not all of them, the problem is that the spacings between the elements of each row are not static, so how can I eliminate all the whitespaces in each row.

My strategy is as follows "simplified":

$command = "sudo ps auxc";
$reply = shell_exec($command.' 2>&1');
//the following line gives an array with a line
//per element
$processArray = explode("\n",$reply);
//the next step "should be to seperate the
//elements in each row obtained according to the
//spaces btw them
<?
for( $i = 0; $i < sizeof($processArray); $i++)
{
//get processID to use as option value
$elements = explode(" ",$processArray[$i]);
$pid = $elements[0];
$user = $elements[1];
$command = $elements[3];
?>




The problem is that the last explode does notwork since the spacing btw in the row differ from one space to many spaces between each element!!
 
Old 12-13-2005, 08:43 AM   #2
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,376

Rep: Reputation: 147Reputation: 147
If each element is a single "word" then you could try extracting them as tokens strtok()
Otherwise you may want to look at regular expressions and preg_split().
 
Old 12-13-2005, 01:03 PM   #3
sirclif
Member
 
Registered: Sep 2004
Location: south texas
Distribution: fedora core 3,4; gentoo
Posts: 192

Rep: Reputation: 30
you can pipe the ouput of your ps command through sed to replace all consecutive white spaces with one space. that may simplify your problem. I'm not familiar with the function explode, but here you go:

$command = "ps auxc | sed 's/\W\+/ /g'"

like i said, this will give you output with only single spaces.
 
Old 12-13-2005, 01:11 PM   #4
ALInux
Member
 
Registered: Nov 2003
Location: Lebanon
Distribution: RHEL 5/CentOS 5/Debian Lenny/(K)Ubuntu Is Dead/Mandriva 10.1
Posts: 668
Blog Entries: 7

Original Poster
Rep: Reputation: 31
Thanks to both of you.......Ive actually used graemef's advice and tried strtok(), I had to create a bi-dimensional array and tweak it a little but in the end it did just what I needed it to do.
Thanks to sirclif too, Iam sure Iam going to make use of his hint too sometime.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
bash cut command to get df percentage nutthick Programming 8 12-16-2010 11:02 AM
simple bash question about cut perfect_circle Programming 2 05-17-2005 11:25 AM
cut , paste or grep ziox Programming 1 12-15-2004 10:51 PM
bash command: grep mAineAc Programming 6 11-18-2004 11:00 AM
tcprobe+grep+cut Axion Programming 1 08-24-2004 05:27 AM


All times are GMT -5. The time now is 10:53 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration