温馨提示:本文翻译自stackoverflow.com,查看原文请点击:shell - convert table into comma separated in text file using bash
awk bash grep sed shell

shell - 使用bash将表转换为逗号分隔在文本文件中

发布于 2020-04-07 11:42:20

我有一个像这样的文本文件:

+------------------+------------+----------+
|     col_name     | data_type  | comment  |
+------------------+------------+----------+
| _id              | bigint     |          |
| starttime        | string     |          |
+------------------+------------+----------+

我如何使用bash获得这样的结果

(_id bigint, starttime string   )

所以只是列名和类型

#remove first 3 lines 
sed -e '1,3d' < columnnames.txt >clean.txt

#remove first character from each line
sed 's/^.//'  < clean.txt >clean.txt

#remove last character from each line
sed 's/.$//' < clean.txt >clean.txt


# remove certain characters 
sed 's/[+-|]//g' < clean.txt >clean.txt 

# remove last line 
sed  '$ d' < clean.txt >clean.txt

所以这就是我到目前为止的情况,如果有更好的实现方法,请告诉我!

查看更多

提问者
hamadkh
被浏览
63
Diego Torres Milano 2020-02-01 03:37

类似的,仅使用awk

awk -F ' *[|]' 'BEGIN {printf("(")} NR>3 && NF>1 {printf("%s%s%s", NR>4 ? "," : "", $2, $3)} END {printf(" )\n")}' columnnames.txt