循环遍历awk中的匹配值 [英] Looping through matched values in awk
问题描述
我正在尝试使用bash解析输入文件,并将值从'variable'提取为'}',但是我不确定如何遍历每个匹配的值并生成预期的输出.任何帮助表示赞赏.
I am trying to parse the input file using bash and extract values from 'variable' to '}' , however I am not sure how to loop over each matched value and generate the expected output. any help is appreciated.
test.sh:
#!/usr/bin/env bash
file="./test.tf"
output="$(awk '/variable/,/}/' $file)"
echo "$output
test.tf:
variable "policy_name1" {
description = "abc xyz"
type = string
default = [
test1,
test2,
]
}
variable "policy_name2" {
description = "aaa xyz"
type = bool
default = false
}
预期输出:
"policyname1","abc xyz","test1,test2"
"policy_name2","aaa xyz","false"
推荐答案
Terraform input files follow the HCL configuration language form (see https://github.com/hashicorp/hcl/blob/hcl2/hclsyntax/spec.md).
该语言支持块,条件等.使用面向行的awk脚本无法正确解析这些文件.通过使用"terraform show -json"输出,您可能会获得更多收益.或者,尝试使用export TF_LOG=trace
设置env var TF_LOG
,然后查看输出
The language has support for blocks, conditional, etc. It is not possible to parse those files correctly using the line-oriented awk script. You might get more mileage by using the 'terraform show -json' output. Alternatively, trying setting the env var TF_LOG
with export TF_LOG=trace
, and reviewing the output
这篇关于循环遍历awk中的匹配值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!