views:

39

answers:

2

Hi there, I currently have

$ echo "`echo "use joomla; describe jos_content" | mysql --batch -u root -ppassword`"

and I would expect output that starts like this

Field\tType\tNull\tKey\tDefault\tExtra\n

but instead I get

Field   Type    Null    Key Default Extra

I have tried all sorts of items at the moment. I could use mysql --html and sed, but, I would only like to go there if this doesn't work out.

thanks!

+1  A: 

Remove the outer echo:

echo "use joomla; describe jos_content" | mysql --batch -u root -ppassword

echoing the output causes all the spaces to be collapsed.

EDIT: MySQL does not output the escaped characters you want (e.g. literal \ then t). In bash (you must use bash's printf), you can get an approximation with:

printf '%q' "$(echo 'use mysql; describe user;'|mysql --batch -u root '-ppassword')"

That will output starting with:

$'Field\tType\tNull\tKey\tDefault\tExtra\n

This is quoting the characters for use with a shell. You can also use sed, as discussed.

Matthew Flaschen
thank you, but that does not display the special characters =/
after the edit there is still some stuff intertwined with the the output. But, I appreciated the responses and taught me about printf.Next time I have a similar issue I will have to look into printf.Thank you again.
+1  A: 

sed is the right tool for this job, not echo.

echo "use joomla; describe jos_content" | mysql --batch -u root -ppassword \
       | sed 's/\t/\\t/g; s/$/\\n/'

Or if you really want all of the output on one line:

echo "use joomla; describe jos_content" | mysql --batch -u root -ppassword \
       | sed 's/\t/\\\\t/g' | while read line ; do echo -n $line\\n ; done

(note the quadruple escape in the sed call).

mobrule
this worked great. I was hoping for something a bit more efficient. But oh well. I've put too much time into this already.Thank you for helping me on my way.
oh, also, what is up with the quadrapole escape!? hehe (I'm curious)
ah, after looking at the escapes I can make more sense of it. Thanks again.
@bonfire, `sed` interprets `\\\\t` as `\\t`. Then, `read` interprets `\\t` as `\t`.
Matthew Flaschen
I'd say `awk` is the right tool, because this is tabular data.
strager