MySQL的HTML表中的替代颜色编码导致shell脚本

时间:2014-03-07 11:30:00

标签: html mysql shell awk

我在shell脚本中使用MySQL查询来获取一些数据。查询就像:

mysql -h$host -u$user -p$pass -H --skip-column-names $database -e "Some_query.sql`" > temp_file

-H选项以HTML表格格式提供结果,但只能在一行中显示。结果采用以下格式:

<TR><TD>2014-03-05</TD><TD>3.869</TD><TD>10.654</TD><TD>6.874</TD><TD>19.194</TD><TD>5.716</TD><TD>15.875</TD></TR><TR><TD>2014-03-04</TD><TD>3.571</TD><TD>10.054</TD><TD>7.874</TD><TD>22.037</TD><TD>5.788</TD><TD>16.432</TD></TR><TR><TD>2014-03-03</TD><TD>3.311</TD><TD>9.180</TD><TD>7.280</TD><TD>20.510</TD><TD>5.533</TD><TD>15.475</TD></TR></TABLE>

我正在尝试为MySql reqult生成的行提供备用颜色代码。

2 个答案:

答案 0 :(得分:0)

不确定它是否有帮助,因为我看不到你的sql,但这里需要考虑......

SELECT CASE WHEN @i:= ABS(@i-1) THEN 'white' ELSE 'black' END colour
     , recipe FROM recipes, (SELECT @i := 0) var 
 ORDER 
    BY recipe;
+--------+-------------------------+
| colour | recipe                  |
+--------+-------------------------+
| white  | Beans on Toast          |
| black  | Cheese & Beans on Toast |
| white  | Cheese on Toast         |
| black  | Macaroni & Cheese       |
| white  | Toast & Jam             |
+--------+-------------------------+

答案 1 :(得分:0)

这对我有用:

cat MySQL_query_result_in_tabular_format.txt | awk '{if (NR % 2 == 1) print "<tr style=\"background:#CCCCCC\"><td>" $1 "</td><td align=\"right\">" $2 "</td><td align=\"right\">" $3 "</td></tr>";else print "<tr style=\"background:#FFFFFF\"><td>" $1 "</td><td align=\"right\">" $2 "</td><td align=\"right\">" $3 "</td></tr>";}' >> HTML_formatted_output.txt