Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 12-13-2005, 09:17 AM   #1
Registered: Nov 2003
Location: Lebanon
Distribution: RHEL 5/CentOS 5/Debian Lenny/(K)Ubuntu Is Dead/Mandriva 10.1
Posts: 670
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, 09:43 AM   #2
Senior Member
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
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, 02:03 PM   #3
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, 02:11 PM   #4
Registered: Nov 2003
Location: Lebanon
Distribution: RHEL 5/CentOS 5/Debian Lenny/(K)Ubuntu Is Dead/Mandriva 10.1
Posts: 670
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.


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 12:02 PM
simple bash question about cut perfect_circle Programming 2 05-17-2005 12:25 PM
cut , paste or grep ziox Programming 1 12-15-2004 11:51 PM
bash command: grep mAineAc Programming 6 11-18-2004 12:00 PM
tcprobe+grep+cut Axion Programming 1 08-24-2004 06:27 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:22 AM.

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