格式化数据表到c++代码

#!/bin/bash
p=""
struct=""
i=0
for line in $(cat $1)
do
pre=$(echo $line | awk '{print $0}' | cut -c 1)
if [[ $pre == "\`" ]]; then
colum=$(echo $line | awk -F "\`" '{print $2}')
if [[ $i == 0 ]];then
p=$p"static const std::string TABLE_NAME=\""$colum"\";"
else
p=$p"\r\n"
struct=$struct"\n"
p=$p"static const std::string FLD_"$(echo $colum | cut -c2- | tr 'a-z' 'A-Z')"=\""$colum"\";"
struct=$struct"std::string str"$(echo $colum | sed 's/'F'/''/g' | sed 's/^\w\|\_\w/\U&/g' | sed 's/\_/''/g')";"
fi
i=$(( $i + 1 ))
fi
done
echo
echo -e $p
echo 
echo -e $struct
echo

 

表格式

CREATE TABLE `t_a` (
  `Fa` int(11) NOT NULL AUTO_INCREMENT COMMENT 'ID',
  PRIMARY KEY (`Fa`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 COMMENT='';

 

//*******************************************************************************

//1去掉F 

//2首字符大写 

//3去掉_

echo "Fhello_o" | sed 's/'F'/''/g' | sed 's/^\w\|\_\w/\U&/g' | sed 's/\_/''/g'

 

//

#!/bin/bash
p=""
struct=""
i=0
maxlen=0

for line in $(cat $1)
do
    pre=$(echo $line | awk '{print $0}' | cut -c 1)
    if [[ $pre == "\`" ]]; then
        colum=$(echo $line | awk -F "\`" '{print $2}')
        if [[ $i == 0 ]];then
            v=1
        else
            left=$(echo $colum | cut -c2- | tr 'a-z' 'A-Z')
            len=${#left}
            if [ $len -gt $maxlen ]; then
                maxlen=$len
            fi
        fi
        i=$(( $i + 1 ))
    fi
done

i=0
for line1 in $(cat $1)
do
    pre=$(echo $line1 | awk '{print $0}' | cut -c 1)
    if [[ $pre == "\`" ]]; then
        colum=$(echo $line1 | awk -F "\`" '{print $2}')
        if [[ $i == 0 ]];then
            text=$( printf "%-"$(( $maxlen-6))"s ="  "" )
            p=$p"static const std::string TABLE_NAME"$text"\""$colum"\";"
        else
            left=$(echo $colum | cut -c2- | tr 'a-z' 'A-Z')
            text=$( printf "%-"$maxlen"s %s=" $left "" )
            p=$p"\r\n"
            struct=$struct"\n"
            p=$p"static const std::string FLD_"$text"\""$colum"\";"
            struct=$struct"std::string str"$(echo $colum | sed 's/'F'/''/g' | sed 's/^\w\|\_\w/\U&/g' | sed 's/\_/''/g')";"
        fi
        i=$(( $i + 1 ))
    fi
done
echo
echo -e "$p"
echo 
echo -e $struct
echo
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值