首页 > 解决方案 > 不包含特定文本的 AWK 打印块

问题描述

我有以下数据文件:

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

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

# [.....loads of code]

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 = {}
}

variable "build_route_tables" {
  description = "List of Route Table keys that need direct internet prior to Egress FW build"
  default = [
    "shared_services",
    "sub_to_afw"
  ]
}

我想做两件事:

我知道我可以像这样打印变量名称:awk '{ gsub("\"", "") }; (/variable/ && $2 !~ /^ARM_/) { print $2}'

我知道我可以使用: 打印代码块awk '/variable/,/^}/',结果是:

# [.....loads of code output before this]

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 = {}
}
variable "build_route_tables" {
  description = "List of Route Table keys that need direct internet prior to Egress FW build"
  default = [
    "shared_services",
    "sub_to_afw"
  ]
}

但是,我不知道如何打印代码块“如果”它们不包含default. 我知道我需要使用一个if语句,也许还有一些变量,但我不确定如何。

此代码块不应出现在我为其获取变量名称的输出中:

variable "build_route_tables" {
  description = "List of Route Table keys that need direct internet prior to Egress FW build"
  default = [
    "shared_services",
    "sub_to_afw"
  ]
}

最终输出不应包含那些具有default

# [.....loads of code output before this]
expressroute_settings
firewall_settings
global_settings
peering_settings
vnet_transit_object
vnet_shared_services_object
route_tables
logging_settings

最好我想保留一个单独的 AWK 命令或文件,没有管道。我对此有一些不喜欢管道的用途。

编辑:更新理想的输出(错过了一些例子default

标签: bashawk

解决方案


OP的问题和评论中的假设和注释收集:

  • }所有变量定义块在新行的第一列以右大括号 ( ) 结尾
  • 我们只显示variable名称(没有双引号)
  • variable如果变量定义的主体包含字符串,我们不显示名称default
  • variable如果它以字符串开头,我们不显示名称ARM_

一个(有点冗长)awk解决方案:

注意:我已将示例输入数据复制到我的本地文件中variables.dat

awk -F'"' '                                              # use double quotes as the input field separator
/^variable / && $2 !~ "^ARM_"  { varname = $2            # if line starts with "^variable ", and field #2 is not like "^ARM_", save field #2 for later display
                                 printme = 1             # enable our print flag
                               }
/variable/,/^}/                { if ( $0 ~ "default" )   # within the range of a variable definition, if we find the string "default" ...
                                    printme = 0          # disable the print flag
                                 next                    # skip to next line
                               }
printme                        { print varname           # if the print flag is enabled then print the variable name and then ...
                                 printme = 0             # disable the print flag
                               }
' variables.dat

这会产生:

logging_settings

推荐阅读