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 ' '
Но это возможно только с sed
, просто используйте глобальную маску, чтобы сначала извлечь символы внутри `, а затем заменить их:
sed 's/.*PARTITIONED BY\(.*\)ROW FORMAT SERDE.*/\1/; s/[^`]*`\([^`]*\)`[^`]*/\1`/g; s/`/ /g; s/ $//;'
Или с помощью sed вы можете l oop с t
до тех пор, пока все замены не будут удалены / сдвинуты:
<<<"$partition_column" sed 's/.*PARTITIONED BY\(.*\)ROW FORMAT SERDE.*/\1/;
# add a newline on the end
s/$/\n/;
:a;
# find something within ` and move it behind the newline
# remove everything in fron ` and after ` that is not a `
s/[^`]*`\([^`]*\)`[^`]*\([^\n]*\n.*\)/\2 \1/;
# loop until the `s` command above does something
ta;
# remove everything in front the newline and the space
s/.*\n //;'