我创建了一个文件,其中包含特定模式中存在的表列表。我需要提取每个表的列详细信息并需要将其写入另一个文件
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
答案2
和awk
:
awk -v OFS="," 'NR==1{ tblName=$2; FS="|"; next }
NF>1 && NR>4{ $1=tblName; gsub(/ *, */, ","); sub(/,$/, ""); print }' infile
输出:
test_table,Name,string,My Name
test_table,Age,string,My Age
我们将从gsub()
每个字段中删除前导和尾随空格;
我们sub()
正在删除结尾的逗号。