Saikiran
Saikiran

Reputation: 189

Print output in vertical format for bash with echo

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)

Upvotes: 2

Views: 1492

Answers (3)

user1934428
user1934428

Reputation: 22291

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

Upvotes: 2

anubhava
anubhava

Reputation: 785611

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))

Upvotes: 2

Ivan
Ivan

Reputation: 7297

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

Upvotes: 2

Related Questions