【问题标题】:Convert tab separated strings to SQL query in bash将制表符分隔的字符串转换为 bash 中的 SQL 查询
【发布时间】:2018-01-28 21:41:30
【问题描述】:

我有一个由制表符分隔的doveadm 命令输出,如下所示:

Username        Quota name      Type    Value   Limit   %
user2@example2.com    User quota      STORAGE 203032  1024000 19
some@example.com    User quota      MESSAGE 816     -       0
other@ex.com    User quota      STORAGE 116873  1024000 11
email@ex.com    User quota      MESSAGE 235     -       0

如何将每一行传输到这样的 SQL 查询?

INSERT INTO quota (username, quota, type, value, limit, percentage) VALUES ('user2@example2.com', 'User quota', 'STORAGE', 203032, 1024000, 19);
...

【问题讨论】:

    标签: string bash command-line debian-based string-operations


    【解决方案1】:

    我会用 awk 说:

    awk -F'\t' 'NR > 1 {printf "INSERT INTO quota (username, quota, type, value, limit, percentage) VALUES (\047%s\047, \047%s\047, \047%s\047, %d, %d, %d);\n", $1, $2, $3, $4, $5, $6}' file
    

    结果将如下所示:

    INSERT INTO quota (username, quota, type, value, limit, percentage) VALUES ('user2@example2.com', 'User quota', 'STORAGE', 203032, 1024000, 19);
    INSERT INTO quota (username, quota, type, value, limit, percentage) VALUES ('some@example.com', 'User quota', 'MESSAGE', 816, 0, 0);
    INSERT INTO quota (username, quota, type, value, limit, percentage) VALUES ('other@ex.com', 'User quota', 'STORAGE', 116873, 1024000, 11);
    INSERT INTO quota (username, quota, type, value, limit, percentage) VALUES ('email@ex.com', 'User quota', 'MESSAGE', 235, 0, 0);
    

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 2010-10-19
      • 2012-11-08
      • 2017-12-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-27
      相关资源
      最近更新 更多