使用 shell 脚本解析 hocon 文件

Parse hocon file using shell script

我有一个从 JSON 文件创建的 hocon 配置。我需要解析以下 hocon 并提取值

示例 hocon 文件: sample.json

    nodes=[
    {
        host=myhostname
        name=myhostname
        ports {
            # debug port
            debug=9384
            # http Port on which app running
            http=9380
            # https Port on which app running
            https=9381
            # JMX port
            jmx=9383
        }
        type=app
        vm-args=[
            "-XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:+PrintClassHistogram",
            "-XX:+UseConcMarkSweepGC -XX:+UseParNewGC ",
            "-XX:+UseTLAB -XX:CMSInitiatingOccupancyFraction=80 -XX:+ExplicitGCInvokesConcurrent -verbose:gc",
            "-XX:SurvivorRatio=8 -XX:+UseNUMA -XX:TargetSurvivorRatio=80 -XX:MaxTenuringThreshold=15",
            "-Xmx3200m -Xms3200m -XX:NewSize=1664m -XX:MaxNewSize=1664m -Xss1024k",
            "-server"
        ]
    }
]
profile=java-dev
resources {
cfg-repository {
    branch-name=master
    commit-id=HEAD
    password=sigma123
    url="http://localhost:9890/gitcontainer/demo-cfg"
    username=sadmin
}
databases=[
    {
        connection-string="oracle03:1522:si12c"
        name=cm
        password=coresmp601
        username=coresmp601cm
    },
    {
        connection-string="oracle03:1522:si12c"
        name=am
        password=coresmp601
        username=coresmp601am
    }
]
idp {
    url="https://sohanb:8097/idp"
}
keystores=[
    {
        file-location="/home/smp/runtime/ssl"
        name=identity
        passphrase=kspass
    }
]
admin {
    password=sigma123
    url="http://punws-sohanb.net:9002/"
    username=sadmin
}
}

现在我想从这个 hocon 文件中提取 vm-args。 我尝试了不同的 bash 工具和 sed/awk 命令,但没有成功。

求推荐!

awk 救援!

 $ awk 'p&&[=10=]~/"/{gsub("\"","");print} /vm-args/{p=1} ' hoconfile

            -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:+PrintClassHistogram,
            -XX:+UseConcMarkSweepGC -XX:+UseParNewGC ,
            -XX:+UseTLAB -XX:CMSInitiatingOccupancyFraction=80 -XX:+ExplicitGCInvokesConcurrent -verbose:gc,
            -XX:SurvivorRatio=8 -XX:+UseNUMA -XX:TargetSurvivorRatio=80 -XX:MaxTenuringThreshold=15,
            -Xmx3200m -Xms3200m -XX:NewSize=1664m -XX:MaxNewSize=1664m -Xss1024k,
            -server

从那里您可以根据需要设置格式。

更新 根据更新的输入文件,您需要通过附加逻辑终止打印,在两个块之间添加 /]/{p=0},如:

$ awk 'p&&[=11=]~/"/{gsub("\"","");print} /]/{p=0} /vm-args/{p=1}' file

您可以将输出通过管道传输到 tr -d ',' | tr -s ' ' 以删除逗号和压缩空格,或者在 awk 脚本中执行相同的操作。

解释: 与 "vm-args" 的模式匹配设置标志 (p=1)。如果设置了标志并且该行包含引号,则打印该行,如果该行与右方括号 (]) 匹配,则将标志关闭 (p=0),因此如果没有更多 "vm-args" 匹配项,则有效停止文件。

更新:我稍微更改了代码,现在将这些行连接成一行,搜索主机名,修剪额外的字符是用 tr 和 sed 完成的。

$ awk 'p && [=12=]~/"/ {args=args [=12=] FS} 
       p && [=12=]~/]/ {print args; exit} 
 /name=myhostname/ {h=1} 
    h && /vm-args/ {p=1}' file | 
 tr -d '",' | 
 tr -s ' ' | 
 sed 's/^ //'

-XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:+PrintClassHistogram -XX:+UseConcMarkSweepGC -XX:+UseParNewGC -XX:+UseTLAB -XX:CMSInitiatingOccupancyFraction=80 -XX:+ExplicitGCInvokesConcurrent -verbose:gc -XX:SurvivorRatio=8 -XX:+UseNUMA -XX:TargetSurvivorRatio=80 -XX:MaxTenuringThreshold=15 -Xmx3200m -Xms3200m -XX:NewSize=1664m -XX:MaxNewSize=1664m -Xss1024k -server

使用这个工具:Hocon Config Printer

对于您的具体示例,您可以使用:

hocon-config-printer sample.hocon.conf  | jq  '.nodes[0]."vm-args"'

输出:

[
  "-XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:+PrintClassHistogram",
  "-XX:+UseConcMarkSweepGC -XX:+UseParNewGC ",
  "-XX:+UseTLAB -XX:CMSInitiatingOccupancyFraction=80 -XX:+ExplicitGCInvokesConcurrent -verbose:gc",
  "-XX:SurvivorRatio=8 -XX:+UseNUMA -XX:TargetSurvivorRatio=80 -XX:MaxTenuringThreshold=15",
  "-Xmx3200m -Xms3200m -XX:NewSize=1664m -XX:MaxNewSize=1664m -Xss1024k",
  "-server"
]