LoginSignup
1
1

More than 3 years have passed since last update.

CSVファイルをSQL結果風に表示するシェルスクリプト

Posted at

このシェルスクリプトを作った経緯は割愛します。

CSVをSQLのテーブルみたいに変換するだけのシェルスクリプトです。(中身ほぼawkです…)

csv_parse_sqldisplay.sh
#!/bin/bash

cat $1 | LANG=C awk -F, '
{
    column_max_length[NF]
    # NF=列数
    # NR=行数
    data[0][NF]

    for (col_num=1; col_num<=NF; col_num++) {
        data[NR][col_num] = $col_num
    }

    if (NR == 1) {
        for (i=1; i<=NF; i++) {
            column_max_length[i] = 0;
        }
    }

    for(i=1; i<=NF; i++) {
        if (length($i) >= column_max_length[i]) {
            column_max_length[i] = length($i)
        }
    }
}
function separator() {
    printf("%s","+")
    for (m in column_max_length) {
        for (j=0; j<column_max_length[m]+2; j++) {
            printf("%s","-")
        }
        printf("%s","+")
    }
    print("")
}
END {
    for (row=1; row<=NR; row++) {
        if (row == 1 || row == 2) {
            separator()
        }
        for (col=1; col<=NF; col++) {
            printf("|")
            space_num = column_max_length[col]-length(data[row][col])
            for (x=0; x<space_num; x++) {
                printf("%s"," ")
            }
            printf(" %s ", data[row][col])
        }
        print("|")
    }
    separator()
}
'

【実行方法】
引数にCSVファイルを与えて実行します。
※CSVに全角文字が入ると表示が崩れます。(良い方法があればコメントいただけると大変嬉しいです。)

実行例
[hoge@local: ~]$ sh csv_parse_sqldisplay.sh test_data.csv
+----+-------------+------------------+---------------+----------------------+--------------------+
| id | postal_code | prefectures_code |  phone_number |                email |          date_time |
+----+-------------+------------------+---------------+----------------------+--------------------+
|  1 |    855-0151 |               18 | 080-3454-3698 |   OK_LI5@example.com | 2012/5/14 23:24:45 |
|  2 |    884-9283 |               14 | 080-5785-8416 |     w4dIMm6@test.com |   1976/9/9 9:12:50 |
|  3 |    105-8176 |               08 | 080-6521-4194 |       i7z6FM@test.jp |  2013/6/17 0:42:10 |
|  4 |    946-3408 |               11 | 090-0974-2227 |   GmeWUoL@sample.org |  2006/6/4 18:54:44 |
|  5 |    540-4290 |               22 | 090-9204-0243 |    PiLueGZht@test.jp | 2001/7/14 15:59:58 |
|  6 |    608-6516 |               14 | 080-2702-9315 | T1siLNEbc@example.jp | 1980/7/14 18:36:40 |
|  7 |    077-4354 |               31 | 090-6172-4143 |      mLgwOL@test.com | 2008/5/12 20:48:12 |
|  8 |    139-6143 |               04 | 090-5286-6431 |    jMzp9O21@test.net | 1993/2/20 23:46:50 |
|  9 |    098-3460 |               41 | 080-5108-5681 |     bMnYW@sample.com | 1992/6/13 17:52:03 |
| 10 |    354-5744 |               02 | 090-8018-7223 |   Ipcjl@sample.co.jp |  2016/5/8 20:26:07 |
+----+-------------+------------------+---------------+----------------------+--------------------+
1
1
0

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
1
1