DDLからメタデータを抽出する

DDLからメタデータを抽出する

特定のスキーマに存在するテーブルのリストを含むファイルを作成しました。各テーブルの列の詳細を抽出し、それを別のファイルに書き込む必要があります。

describe test_table
+-----------+------------+------------+
| col_name  | data_type  |Comment     |
+-----------+------------+------------+
| Name      | string     |My Name     |
| Age       | string     |My Age      |
+-----------+------------+------------+

出力ファイルには、以下の詳細を含める必要があります。

test_table,Name,String,My Name
test_table,Age,string,My Age

ベストアンサー1

$ cat extract-columns.pl
#!/usr/bin/perl -l

while(<>) {
  # Is the current line a "describe" line?
  if (m/describe\s+(.*)/i || eof) {
    $table = $1;
    next;
  };

  # skip header lines, ruler lines, and empty lines
  next if (m/col_name|-\+-|^\s*$/);

  # remove pipes and spaces at beginning and end of line
  s/^\|\s*|\s*\|\s*$//g;

  # remove spaces surrounding pipe characters
  s/\s*\|\s*/|/g;

  # extract field name by splitting input line on pipe chars
  my ($name, $type, $comment) = split /\|/;
  print join(",", $table, $name, $type, $comment);
}

出力例:

$ ./extract-columns.pl table2.txt 
test_table,Name,string,My Name
test_table,Age,string,My Age

おすすめ記事