使用bash unix从变量中提取子字符串(可能使用模式匹配?)>

问题描述 投票:0回答:2

我在变量中有一个值,如:

partition_column='| PARTITIONED BY ( | | `part_col1` int, | | `part_col2` int) | | ROW FORMAT SERDE |'

我想在PARTITIONED BY和ROW FORMAT SERDE之间提取值,在上面的情况下,它是part_col1和part_col2

Desired output:
part_col1 part_col2

我尝试了许多命令,似乎没有任何作用:

result=$(echo $par_col | sed -nr '/`/p'|  cut -d '`' -f 2|xargs -n 1 echo -n "")

您能否更正以上命令,或提出其他建议?

我在变量中具有一个值,例如:partition_column ='|分区方式(| |`part_col1` int,| |`part_col2` int)| |行格式序列|'我想提取PARTITIONED BY和ROW之间的值...

bash
2个回答
0
投票

假设您已经安装了GNU cut,那么以下内容将在bash中起作用:


0
投票
partition_column='| PARTITIONED BY ( | | `part_col1` int, | | `part_col2` int) | | ROW FORMAT SERDE |'
# extract everything between the patterns
<<<"$partition_column" sed 's/.*PARTITIONED BY\(.*\)ROW FORMAT SERDE.*/\1/' |
  # replace spaces for newlines
  tr ' ' '\n' |
  # filter only lines starting with \`
  grep '^`' |
  # remove the \`
  sed 's/`//g' |
  # join lines using a space
  paste -sd ' '
© www.soinside.com 2019 - 2024. All rights reserved.