通过bash解析db2look 导出的sql文件中的create table语句,并写入对应的独立的文件。
脚本如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#!/bin/bash target_dir=$1 while read line; do [[ "$line" == "" ]] && continue [[ $line == CREATE\ TABLE\ * ]] && { read schema table < <(awk -F'"' '{print $2 " " $4}' <<<"$line") #echo $schema $table dir="$target_dir/$schema" [ -d $dir ] || mkdir -p $dir file=$dir/${table}.sql echo "$line" >$file while read line; do [[ "$line" == IN\ * ]] && [[ "$line" != *\; ]] && continue # infact IN clause always not end with ; echo " $line" >>$file # Space indentation looks better [[ $line == *\; ]] && break done } done |
其实,grep也能提取,只是不方便分文件写入,而且,不方便去掉不想要的IN子句,grep的写法:
1 |
cat ifm30.sql |grep --binary-files=text -Pzo "^CREATE TABLE .+[^;]+;" |