Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.9k views
in Technique[技术] by (71.8m points)

parsing the json kind of file to find out particular values using unix shell script/Python

I would like to extract few lines of given file using shell script. The problem is I have set of files which need to parsed and extract data. The below examples explains my scenario: I am looking to extract only "source" and "destination" values from the given files.


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"
}
]

My expected output would be

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you can modify the list to a valid json format (as below) and python is your option, please try:

#!/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"]))

Output:

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

Assuming you have a HOCON file file.conf which looks like:

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
}
]

Then try to execute the python script:

#!/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']))

Output:

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

If you want to parse multiple files in multiple directories recursively, save the following script as something like 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

Then run:

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

The command line above assumes the HOCON files have .conf extension and the variable $dir holds the top directory path of the conf files. Please modify them according to your environment.

EDIT3
The python script above may be altered by:

#!/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']))

which will be a bit smarter.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...