行列转换在数据库,开发语言中都是一个津津乐道的话题,今天来简单演示一个使用sed所作的特殊行列转换。 日志的内容如下: append data from MIG_TEST.MO1_MEMO_EXT_2 to MIG_TEST.MO1_MEMO 1936470 rows created. Elapsed: 00:01:01.53 Commit complete. Elapsed: 00:00:00.03 append data from MIG_TEST.MO1_MEMO_EXT_3 to MIG_TEST.MO1_MEMO 873897 rows created. Elapsed: 00:00:22.67 Commit complete. Elapsed: 00:00:00.02 append data from MIG_TEST.MO1_MEMO_EXT_4 to MIG_TEST.MO1_MEMO 1702297 rows created. Elapsed: 00:00:57.67 Commit complete. Elapsed: 00:00:00.04 append data from MIG_TEST.MO1_MEMO_EXT_5 to MIG_TEST.MO1_MEMO 1280364 rows created. Elapsed: 00:00:52.77 现在想把特定的一些行转列,实现类似如下的效果。 这样数据条数,插入时间等就一目了然了,可以在此基础上成为报告,便于查看状态和监控。 append data from MIG_TEST.MO1_MEMO_EXT_1 to MIG_TEST.MO1_MEMO 1661924 rows created. Elapsed: 00:01:05.55 append data from MIG_TEST.MO1_MEMO_EXT_2 to MIG_TEST.MO1_MEMO 1936470 rows created. Elapsed: 00:01:01.53 append data from MIG_TEST.MO1_MEMO_EXT_3 to MIG_TEST.MO1_MEMO 873897 rows created. Elapsed: 00:00:22.67 这个时候可以使用sed来做处理,完成特定的行列转换。 首先删除多余的空行 sed -e '/^$/d' append_details.log 然后在此基础上根据”append data from“来做第一次行列转换。 sed '/append data from/{N;N; s/\n/ /}' 这样,格式化后的内容就类似下面的形式。 append data from MIG_TEST.MO1_MEMO_EXT_11 to MIG_TEST.MO1_MEMO 2083618 rows created. Elapsed: 00:01:24.92 Commit complete. Elapsed: 00:00:00.08 append data from MIG_TEST.MO1_MEMO_EXT_12 to MIG_TEST.MO1_MEMO 976772 rows created. Elapsed: 00:00:32.67 Commit complete. Elapsed: 00:00:00.05 append data from MIG_TEST.MO1_MEMO_EXT_13 to MIG_TEST.MO1_MEMO 851574 rows created. Elapsed: 00:00:30.06 Commit complete. Elapsed: 00:00:00.02 然后再做一次行列转换,按照关键字“rows created" 来做行列转换 最后的输入类似下面。 append data from MIG_TEST.MO1_MEMO_EXT_1 to MIG_TEST.MO1_MEMO 1661924 rows created. Elapsed: 00:01:05.55 append data from MIG_TEST.MO1_MEMO_EXT_2 to MIG_TEST.MO1_MEMO 1936470 rows created. Elapsed: 00:01:01.53 append data from MIG_TEST.MO1_MEMO_EXT_3 to MIG_TEST.MO1_MEMO 873897 rows created. Elapsed: 00:00:22.67 append data from MIG_TEST.MO1_MEMO_EXT_4 to MIG_TEST.MO1_MEMO 1702297 rows created. Elapsed: 00:00:57.67 append data from MIG_TEST.MO1_MEMO_EXT_5 to MIG_TEST.MO1_MEMO 1280364 rows created. Elapsed: 00:00:52.77 完整的命令如下: sed -e '/^$/d' append_details.log|sed '/append data from/{N;N; s/\n/ /}' |sed '/rows created/{N;s/\n/ / }'|grep 'append'