在shell脚本中将字符串转换为xml?

时间:2011-03-22 11:56:07

标签: mysql ruby-on-rails xml shell

我编写了一个从数据库中获取数据的脚本。

现在我想增强该脚本以创建一个包含数据库返回数据的xml文件。

如何将该字符串转换为shell脚本中的xml文件。

script.sh

#!/bin/sh
echo 'SELECT TABLE_NAME AS "Table Name", table_rows AS "Quant of Rows", ROUND((data_length + index_length)/1024/1024,2) AS "Total Size Mb" FROM information_schema.TABLES WHERE information_schema.TABLES.table_schema="database_name";' | mysql -u root -pmysql

从数据库获取数据

"Table Name\tQuant of Rows\tTotal Size Mb\ntable_1\t2\t0.02\ntable_2\t1\t0.02\ntable_3\t142\t0.02\ntable_4\t50839\t5.03\ntable_5\t16573\t5.13\ndlr\t0\t0.02\ntable_6\t6\t0.02\ntable_7\t0\t0.03\ntable_8\t2\t0.08\ntable_9\t4\t0.02\n"

我想在脚本中编写一些逻辑,将此字符串转换为xml文件。

我怎么能这样做?处理这种情况的首选方法是什么?

2 个答案:

答案 0 :(得分:1)

尝试这样的事情 -

shell> mysql --xml -e "SELECT * FROM test.person" > person-dump.xml

- xml选项产生类似XML的输出。

MySQL command-linel client ref.

答案 1 :(得分:0)

#!/bin/sh
mysql -u root -pmysql --xml > /home/test/Desktop/temp.xml << eof
SELECT TABLE_NAME AS "Table Name", table_rows AS "Quant of Rows", ROUND((data_length + index_length)/1024/1024,2) AS "Total Size Mb"  FROM information_schema.TABLES WHERE information_schema.TABLES.table_schema='database_name';
eof