24
23

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

csvファイルからhtmlの表を作る

Last updated at Posted at 2013-03-28

仕事で自動テストを利用しているのですが、結果が見にくかったので、シェルでhtml化する方法を調べてみました。テストの実行結果などが記載されているcsvファイルはあるとして、そのファイルを入力してhtmlの表を作る方法です。

下記のようなcsvファイルがあったとします。

項番,シナリオ名,テスト結果
1,test_1, OK
2,test_2, OK
3,test_3, NG
4,test_4, OK

下記スクリプトは、↑のようなcsvファイルを入力としてhtmlの表を出力します。
引数で指定されたcsvファイルの内容をhtml_bodyパラメータに設定して、cat以降でindex.htmlファイルへ出力しています。

sample.sh
#!/bin/sh

html_body="<table border=1>"
while read line; do
	str="<tr>"
	IFS=','
	set -- $line
	for col do
		str="${str}<td>$col</td>"
	done
	str="${str}</tr>"
	html_body="${html_body}${str}"
done < "$1"
html_body="${html_body}</table>"

cat << EOF > ./index.html
<!DOCTYPE html>
<html>
<head><meta charset="UTF-8" /></head>
<body>
	$html_body
</body>
</html>
EOF

こんな感じのスクリプトを作っておくと、自動テストとかの結果も見やすくていいです。

24
23
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
24
23

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?