开发者

Get line counts of INSERT's by tablename from mysqldump - awk, sed, grep

开发者 https://www.devze.com 2023-01-31 07:51 出处:网络
bash master needed... To compare mysqldumps from multiple dates, I need sql GROUP BY, ORDER BY functionality... but on the command line...

bash master needed...

To compare mysqldumps from multiple dates, I need sql GROUP BY, ORDER BY functionality... but on the command line...

Using grep / sed / awk I need to look for all INSERT statements, then export a line count per tablename. I'd real开发者_StackOverflowly love a byte count per tablename too...

A typical line looks like this:

INSERT INTO `admin_rule` ...

match the INSERT, then match the tablename in ``, counting by unique tablename


How about this little awk snippet:

BEGIN   { FS="`" }
/^INSERT/ { count[$2]+=1; bytes[$2]+=length($0) }
END { for(table in count) print table "," count[table] "," bytes[table]; }

Edit: test case here:

$ cat test.sql
INSERT INTO `t1` VALUES('a', 12, 'b');
INSERT INTO `t2` VALUES('test', 'whatever', 3.14);
INSERT INTO `t3` VALUES(1, 2, 3, 4);
INSERT INTO `t2` VALUES('yay', 'works', NULL);
INSERT INTO `t2` VALUES(NULL, 'something' 2.71);
INSERT INTO `t3` VALUES(5, 6, 7, 8);
INSERT INTO `t5` VALUES('beta', 'gamma');
INSERT INTO `t6` VALUES('this', 'is', 'table', 'six');
$ awk -f t.awk <test.sql
t5,1,41
t6,1,54
t1,1,38
t2,3,144
t3,2,72
0

精彩评论

暂无评论...
验证码 换一张
取 消