gpt-3.5-turbo-0613· openai
Suzhen/CodeChat
Write a Bash function that parses a Terraform map and extracts keys at the second level
- User
Write a Bash function that parses a Terraform map and extracts keys at the second level
- Assistant
Here's a Bash function that takes a Terraform map as input, and extracts the keys at the second level:
parse_terraform_map() { local map="$1" local keys=() # Loop through the map while IFS== read -r key value; do # Check if the value is another map if [[ $value =~ ^{.*}$ ]]; then # Extract keys at the second level while IFS== read -r sub_key sub_value; do keys+=("$sub_key") done < <(echo "$value" | awk -F= '{print $1 "=" $2}') fi done < <(echo "$map" | awk -F= '{print $1 "=" $2}') # Print the extracted keys for key in "${keys[@]}"; do echo "$key" done }To use this function, you can pass a Terraform map as a string argument. For example:
$ my_map='{ "key1" = {} , "key2" = { "sub_key1" = "value1" , "sub_key2" = "value2" } }' $ parse_terraform_map "$my_map" sub_key1 sub_key2The function will print each extracted key on a separate line. Note that this function assumes that the Terraform map is in valid JSON format.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.