1

I am trying to get the partition column names of a hive table in bash using grep and regex. I am trying this:

hive -e 'show create table employees'  | grep -E 'PARTITIONED BY (.*)'

This is giving me the result like: PARTITIONED BY (

How do I have to modify my command to grab this part:

PARTITIONED BY ( year char(4), month char(2))

The end goal for me is to grab all the partition columns into variables or array so that I can use them later. If there is a better way to achieve what I am trying to do, I am open to that as well.

Any help would be appreciated.

Thank you.

Hemanth
  • 705
  • 2
  • 16
  • 32

1 Answers1

1

You may use

hive -e 'describe formatted employees' | \
  awk '/Partition/{p=1; next} /Detailed/{p=0} p' | \
    grep -oE '^[[:alnum:]_]+'

The describe formatted employees will yield a better formatted output describing the table, awk '/Partition/{p=1; next} /Detailed/{p=0} p' will extract the block of text that is between Partition and Detailed excluding the matching lines and grep -oE '^[[:alnum:]_]+' will fetch only those words that are at the start of the lines and only contain letters or digits. If there can only be letters replace [:alnum:] with [:alpha:].

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563