Print output in vertical format for bash with echo

Viewed 689

I have 3 variables defined and using echo to display the output. But the output displays in single line, expected output is sideby side.

a=$(ps -ef | grep java) 
b=$(ps -ef | grep http)  
c=$(ps -ef | grep php)
echo $a $b $c

Output :

java1 java2 java3 java4 http1 http2 http3 http4 php1 php2 php3 php4

Expected output

 java1 http1 php1
 java2 http2 php2 
 java3 http3 php3  
 java4 http4 php4

also used paste as below, but that dint work too. Using solaris OS

paste <(echo $a) <(echo $b) <(echo $c)

3 Answers

Using paste in process substitution you may do something like this:

paste <(pgrep java) <(paste <(pgrep http) <(pgrep php))

ps -ef on most of Unix flavors gives multi column output not just a single column output.

Using ps -ef you may be able to do this:

paste <(ps -ef | grep -o java) <(paste <(ps -ef | grep -o http) <(ps -ef | grep -o php))

Some thing like this may help

#!/bin/bash

a='java1 java2 java3 java4'
b='http1 http2 http3 http4'
c='php1  php2  php3  php4'
a=($a)
b=($b)
c=($c)

for i in ${!a[@]}; {
    echo ${a[$i]} ${b[$i]} ${c[$i]}
}

Usage

$ ./test 
java1 http1 php1
java2 http2 php2
java3 http3 php3
java4 http4 php4

How about

for s in $a $b $c
do
    echo $s
done

And if you don't need the variables later on, I would have simply written

for w in java http php
do
    ps -ef | grep -F -- "$w"
done
Related