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 ' '
But it's possible with only sed, just use a global mask to first extract the chars within `, then replace them:
sed 's/.*PARTITIONED BY\(.*\)ROW FORMAT SERDE.*/\1/; s/[^`]*`\([^`]*\)`[^`]*/\1`/g; s/`/ /g; s/ $//;'
Or with sed you can loop with t until all the replacements are removed/shifted:
<<<"$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 //;'
partition_column=....is incorrect (did you ever try to copy and paste it to the command line), since it would interpretpart_col1as the name of a command, and tries to run it.