开发者

bash: output \n \t characters from mysql query

I currently have

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

and I would expect output that starts like 开发者_如何学JAVAthis

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!


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜