【问题标题】:parsing the json kind of file to find out particular values using unix shell script/Python使用 unix shell 脚本/Python 解析 json 类型的文件以找出特定值
【发布时间】:2021-04-17 17:00:43
【问题描述】:

我想使用 shell 脚本提取几行给定文件。问题是我有一组需要解析和提取数据的文件。下面的示例解释了我的场景:我希望从给定文件中仅提取“源”和“目标”值。


lists = [ 
{
name = "Account - UU ",
source = "1-account",
destination = "account-hhh",
other = "111111"
},
{
name = "Account - PP,
source = "2-account",
destination = "account-hhh12345",
other = "1212"
},
{
name = "Account - GG ",
source = "3-account",
destination = "account-gg567",
other = "44444"
},
{
name = "Account - QQ,
source = "4-account",
destination = "account-manager123456",
other = "23232323"
}
]

我的预期输出是

source = "1-account" | destination = "account-hhh"
source = "2-account" | destination = "account-hhh12345"
source = "3-account" | destination = "account-gg567"
source = "4-account" | destination = "account-manager123456"

【问题讨论】:

  • 请edit你的问题表明你已经尝试过。我很确定一个快速的“谷歌”会告诉你如何做到这一点。你有或者可以安装一个 json 解析器,比如jq?
  • 它不是一个有效的 json 文件。

标签: shell unix awk


【解决方案1】:

如果您可以将列表修改为有效的 json 格式(如下所示)并且python 是您的选择, 请尝试:

#!/usr/bin/python

lists = [
{
"name" : "Account - UU",
"source" : "1-account",
"destination" : "account-hhh",
"other" : 111111
},
{
"name" : "Account - PP",
"source" : "2-account",
"destination" : "account-hhh12345",
"other" : 1212
},
{
"name" : "Account - GG",
"source" : "3-account",
"destination" : "account-gg567",
"other" : 44444
},
{
"name" : "Account - QQ",
"source" : "4-account",
"destination" : "account-manager123456",
"other" : 23232323
}
]

for i in lists:
    print('source = "%s" | destination = "%s"' % (i["source"], i["destination"]))

输出:

source = "1-account" | destination = "account-hhh"
source = "2-account" | destination = "account-hhh12345"
source = "3-account" | destination = "account-gg567"
source = "4-account" | destination = "account-manager123456"

编辑

假设您有一个 HOCON 文件 file.conf,如下所示:

lists = [
{
name = "Account - UU",
source = "1-account",
destination = "account-hhh",
other = 111111
},
{
name = "Account - PP",
source = "2-account",
destination = "account-hhh12345",
other = 1212
},
{
name = "Account - GG",
source = "3-account",
destination = "account-gg567",
other = 44444
},
{
name = "Account - QQ",
source = "4-account",
destination = "account-manager123456",
other = 23232323
}
]

然后尝试执行python脚本:

#!/usr/bin/python

from pyhocon import ConfigFactory

conf = ConfigFactory.parse_file('./file.conf')
for i in conf['lists']:
    print('source = "%s" | destination = "%s"' % (i['source'], i['destination']))

输出:

source = "1-account" | destination = "account-hhh"
source = "2-account" | destination = "account-hhh12345"
source = "3-account" | destination = "account-gg567"
source = "4-account" | destination = "account-manager123456"

EDIT2

如果要递归解析多个目录中的多个文件,请将以下脚本另存为myscript.py:

#!/usr/bin/python

from pyhocon import ConfigFactory
import sys

conf = ConfigFactory.parse_file(sys.argv[1])
try:
    if type(conf['lists']) is list:
        for i in conf['lists']:
            print('source = "%s" | destination = "%s"' % (i['source'], i['destination']))
except Exception:
    pass

然后运行:

find "$dir" -name "*.conf" -type f -exec python myscript.py {} \;

上面的命令行假设 HOCON 文件有 .conf 扩展名,变量 $dir 保存 conf 文件的顶级目录路径。请根据您的环境修改它们。

EDIT3
上面的 python 脚本可以通过以下方式更改:

#!/usr/bin/python

from pyhocon import ConfigFactory
import sys

conf = ConfigFactory.parse_file(sys.argv[1])
if 'lists' in conf.keys():
    for i in conf['lists']:
        print('source = "%s" | destination = "%s"' % (i['source'], i['destination']))

这会更聪明一点。

【讨论】:

  • 是的,我正在寻找一个解决方案,如果它可用的话,也可以在 python 中使用。基本上我得到的是它作为 conf 文件(Hocon)。我如何使用 python 将其称为文件而不是变量?基本上我在一个目录中获取这些示例文件,我需要解析这些文件并找到源和目标
  • 感谢您的反馈。我已经用另一个版本更新了我的答案,它按原样读取 conf 文件,内部转换为 json 格式,然后输出相同的结果。试试看好吗?
  • 谢谢,如前所述,我的文件是 Hocon 文件。看起来我们需要使用 hocon 解析器 - stackoverflow.com/questions/53188635/… 知道如何使它可行吗?
  • 感谢您提供的信息。你能提供一些示例文件吗?还是以提供的为例还可以吗?
  • 谢谢,示例文件与问题中给出的完全相同
猜你喜欢
  • 1970-01-01
  • 2023-03-30
  • 1970-01-01
  • 1970-01-01
  • 2016-06-21
  • 1970-01-01
  • 2012-01-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多