tags:

views:

52

answers:

4

The following code is working as expected. But I can not format the output. It will print something like this:

mysql
test
someDB

I want the output on a single line

mysql test someDB

I tried using sed in the script but it did not work.

#!/bin/sh
for dbName in `mysqlshow -uroot -pPassWord | awk '{print $2}'`
do
echo "$dbName" | egrep -v 'Databases|information_schema';
done
+1  A: 

The newline is generated by the echo command most likely, the following should do the same without the newlines (not tested)

mysqlshow -uroot -pPassWord | awk '{print $2}' | egrep -v 'Databases|information_schema'

and has the added bonus of spawning just 1 grep instead of 3 grep processes.

rsp
Isn't awk printing newlines, too?
Dennis Williamson
even better...just one awk command: mysqlshow -uroot -pPassWord| awk '!/Databases|information_schema/{print $2}'
ghostdog74
+2  A: 

whenever you want to combine all lines of output into one you can also use xargs:

e.g.

find 
.
./zxcv
./fdsa
./treww
./asdf
./ewr

becomes:

find |xargs echo
. ./zxcv ./fdsa ./treww ./asdf ./ewr
+1  A: 

To do a variation combining naumcho's and rsp's answers that will work for small numbers of results:

echo $(mysqlshow -uroot -pPassWord | awk '{print $2}' | egrep -v 'Databases|information_schema')
Dennis Williamson
+1  A: 

you can use tr to get your output to one line

<output from somewhere> | tr "\n" " "
ghostdog74