如果您可以将列表修改为有效的 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']))
这会更聪明一点。