1

I have a doveadm command output separated by tabs, which looks like this:

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

How can I transporm each line to SQL query like this?

INSERT INTO quota (username, quota, type, value, limit, percentage) VALUES ('user2@example2.com', 'User quota', 'STORAGE', 203032, 1024000, 19);
...
Sfisioza
  • 3,830
  • 6
  • 42
  • 57

1 Answers1

1

I'd say with 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

The result will look like:

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);

Hope this helps.

tshiono
  • 21,248
  • 2
  • 14
  • 22