bash - 不包含特定文本的 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"
]
}
我想做两件事:
- 打印不带引号的变量名
- 如果代码块不包含,仅打印变量名称
default
我知道我可以像这样打印变量名称: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
)
解决方案
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
推荐阅读
- javascript - 在资源文件与 package.json 中配置 JS 库的优点/缺点是什么?
- cmake - 在 Ninja 中使用 ExternalProject 下载步骤
- android - 使 RecyclerView + Button 滚动它一块// 不显示所有禁用nestedScrolling 的项目
- angular - Angular Typescript 错误尝试引用已实现的类属性
- javascript - AngularJS .value():没有在控制器中获取更新/修改的值
- javascript - 使用 javascript 在 chrome 中播放 .ts 文件
- java - 订购我的 arrayLust 时遇到问题
- saml-2.0 - ADFS 3,创建规则以将组成员身份作为声明发送
- python - 大文件中的朴素python字符串格式(常见和非常见部分)
- javascript - 平移或缩放时获取地图的纬度