Conclusion on one line

The following code works as expected. But I cannot format the output. It will print something like this:

mysql
test
someDB

      

I need the output on one line

mysql test someDB

      

I tried to use sed in the script, but it didn't work.

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

      

+2


source to share


4 answers


To implement a change combining naumcho's and rsp's answers that will work for a small number of results:



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

      

+1


source


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

eg.

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

      



becomes:

find |xargs echo
. ./zxcv ./fdsa ./treww ./asdf ./ewr

      

+10


source


you can use tr to print your output on one line

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

      

+5


source


Newline is generated by echo command, most likely next line should do the same without newlines (not checked)

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

      

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

+1


source







All Articles