根据建表语句,将字段由驼峰命名(camelCase)转换为下划线命名(snake_case)

#!/bin/bash
# 根据建表语句,将字段由驼峰命名(camelCase)转换为下划线命名(snake_case)
# 连续的大写字母只在第一个字段前添加下划线
flag=" "

grep -Ev "CREATE TABLE|PRIMARY KEY|ENGINE=InnoDB" tmpa|awk '{print $1,$NF}'|sed "s/[\'\`,]//g"|while read line
do
    column=$(echo ${line}|awk '{print $1}')
    name_cn=$(echo ${line}|awk '{print $2}')
    columns_snake_case=$(echo "${column}" | sed 's/\([A-Z]\)\([A-Z]*\)/_\L\1\2/g' | sed 's/^_//'|sed 's/__/_/g')
    printf "    %s%-50s as %-50s -- %s\n" "${flag}" "${column}" "${columns_snake_case}" "${name_cn}"
    flag=","
done

posted @ 2024-10-11 17:00  chenzechao  阅读(19)  评论(0编辑  收藏  举报