【问题标题】:AWK create internal variable array from results and print in END blockAWK 从结果创建内部变量数组并在 END 块中打印
【发布时间】:2021-03-25 23:02:55
【问题描述】:

我有以下variables.tf 文件:

variable "ARM_CLIENT_ID" {
  description = "Client ID for Service Principal"
}

variable "ARM_CLIENT_SECRET" {
  description = "Client Secret for Service Principal"
}

variable "ARM_ENVIRONMENT" {
  description = "The deployment environment acronym to which the resources are being deployed"
}

variable "ARM_LOCATION" {
  description = "The geo location to which the resources are being deployed"
}

variable "ARM_SUBSCRIPTION_ID" {
  description = "Azure Subscription for deployment"
}

variable "ARM_TENANT_ID" {
  description = "Azure AD tenant identifier for Service Principal"
}

variable "expressroute_settings" {
  description = "(Required) ExpressRoute Gateway and Public IP settings"
}

variable "firewall_settings" {
  description = "(Required) ExpressRoute Gateway and Public IP settings"
}

variable "global_settings" {
  description = "Global settings for subscription"
}

variable "peering_settings" {
  description = "(Required) vNet Peering configuration for shared services vnets"
}

variable "vnet_transit_object" {
  description = "(Required) configuration object describing the transit networking configuration, as described in README"
}

variable "vnet_shared_services_object" {
  description = "(Required) configuration object describing the shared services networking configuration, as described in README"
}

variable "route_tables" {
  description = "Routes for subnets declared in tfvars"
}

variable "logging_settings" {
  description = "Logging settings from TFVARs"
}

variable "azure_firewall_nat_rule_collections" {
  default = {}
}

variable "azure_firewall_network_rule_collections" {
  default = {}
}

variable "azure_firewall_application_rule_collections" {
  default = {}
}

AWK print block that does NOT contain specific text 的帮助下,我能够提出以下 AWK 程序,只要结果块中不包含单词 default,它就可以帮助我打印出结果。

BEGIN   {
    string = "route_tables,global_settings"
    regex = "[,]"
    n = split ( string, arr, regex )
}
{ gsub("\"", "") }
/^variable/     {   varname = $2
                    printme = 1
                }
                {
                    for ( i=1; i<=n; i++ )
                        if ( $2 ~ arr[i] )
                            printme = 0
                            myarr[i] = $2
                }
/variable/,/^}/ {   if ( $0 ~ "default" )
                        printme = 0
                    next
                }
printme         {   printf varname " = \"\"\n"
                    printme = 0
                }
END {
    for ( x=1; x<=n; x++ )
        print myarr[x]
}

结果如下:

ARM_CLIENT_ID = ""
ARM_CLIENT_SECRET = ""
ARM_ENVIRONMENT = ""
ARM_LOCATION = ""
ARM_SUBSCRIPTION_ID = ""
ARM_TENANT_ID = ""
expressroute_settings = ""
firewall_settings = ""
peering_settings = ""
vnet_transit_object = ""
vnet_shared_services_object = ""
logging_settings = ""


注意;最后的两个额外的空白行,这是由于我无法正常工作的新变量数组引起的。

(我知道我在上面链接中的原始问题中确实说过我不想包含ARM_,但情况仍然如此,但为了进行有效测试,我没有将其包含在我当前的 AWK 程序中)

我正在尝试扩展这个想法,以便创建一个名为myarr 的变量数组,并添加来自变量string 的值(如果它们在文件中找到)。然后在END 块中,我可以将它们打印为列表。

为什么要打印出来?所以我可以在上面附加一些文本,就像我对varname 打印所做的那样。

编辑:为了阐明我希望它如何输出,我创建了这个虚拟文本文件。我想将字符串中的值输出到 END 块(在处理完所有内容之后),希望通过变量数组(如果这是正确的做法?)

【问题讨论】:

  • 所以你只是想停止在 END 块中打印空行?
  • @RamanSailopal;希望我对图片的编辑澄清了我希望输出的方式

标签: bash awk


【解决方案1】:

您遇到的问题是if 语句和for 循环周围缺少{}

BEGIN {
  string = "route_tables,global_settings"
  regex = "[,]"
  n = split ( string, arr, regex )
}
{ 
  gsub("\"", "")
}
/^variable/ {
  varname = $2
  printme = 1
}
{
  for ( i=1; i<=n; i++ ) {
    if ( $2 ~ arr[i] ) {
      printme = 0
      myarr[i] = $2
    }
  }
}
/variable/,/^}/ {
  if ( $0 ~ "default" )
     printme = 0
     next
}
printme {
  printf varname " = \"\"\n"
  printme = 0
}
END        {
  for ( x=1; x<=n; x++ ) {
    print myarr[x]
  }
}
ARM_CLIENT_ID = ""
ARM_CLIENT_SECRET = ""
ARM_ENVIRONMENT = ""
ARM_LOCATION = ""
ARM_SUBSCRIPTION_ID = ""
ARM_TENANT_ID = ""
expressroute_settings = ""
firewall_settings = ""
peering_settings = ""
vnet_transit_object = ""
vnet_shared_services_object = ""
logging_settings = ""
route_tables
global_settings

【讨论】:

  • 如果你把这些缩进整理成2个或4个空格会更容易阅读。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-31
  • 2022-01-08
  • 2010-12-22
  • 2021-05-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多