Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Shell 我想编写一个unix代码,从.txt文件中的SQL代码中提取括号之间的所有列名_Shell_Unix - Fatal编程技术网

Shell 我想编写一个unix代码,从.txt文件中的SQL代码中提取括号之间的所有列名

Shell 我想编写一个unix代码,从.txt文件中的SQL代码中提取括号之间的所有列名,shell,unix,Shell,Unix,我想在unix中编写一个动态代码,它将提取括号中包含的任意数量的列名 CREATE TABLE `test`( `id` int, `name` string, `email` string, `signup` date) SPLIT BY ( `country` string) CLUSTERED BY ( name) 预期产出将是: ( `id` int, `name` string, `email` string, `signu

我想在unix中编写一个动态代码,它将提取括号中包含的任意数量的列名

CREATE TABLE `test`(
   `id` int,
   `name` string,
   `email` string,
   `signup` date)
SPLIT BY (
   `country` string)
CLUSTERED BY (
   name)
预期产出将是:

(
   `id` int,
   `name` string,
   `email` string,
   `signup` date)

一个适用于特定输入的非常简单的示例可能如下所示:

printing=0
create_table_start_re='CREATE[[:space:]]+TABLE[[:space:]]+[^(]+([(].*)'
close_paren_re='^(.*[)])'
while IFS= read -r line; do
  if [[ $line =~ $create_table_start_re ]]; then
    printf '%s\n' "${BASH_REMATCH[1]}"
    printing=1
    continue
  elif (( printing == 0 )); then
    continue
  elif [[ $line =~ $close_paren_re ]]; then
    printf '%s\n' "${BASH_REMATCH[1]}"
    printing=0
  else
    printf '%s\n' "$line"
  fi
done
…正如您可以看到的,在


然而,这决不是所有可能的有效SQL都能正常工作!如果您想要能够解析SQL DDL的东西,那么应该使用真正的专用解析器,而不是使用面向行的文本处理工具来进行解析。

为什么要在问题中指定“使用grep”?如果有人可以使用awk或sed编写更干净(或者更便携,只要
grep-o
是GNUism)的代码,那么你为什么在写问题之前就拒绝回答这个问题呢?顺便说一句,看起来你要的是列名,而不是表名。非常抱歉,这是我第一次发布问题,我会编辑并澄清。你需要正确处理所有可能的合法SQL语法,还是只处理问题中显示的子集(换行符位于同一位置)?我不在乎换行符或语法,我只想提取列名。