我有一个.json
文件需要解析:
{
"schemaVersion":16,
"addons":[
{"id":"testpilot@labs.mozilla.com","syncGUID":"pc9SmMiK6Peu","location":"app-profile","version":"1.2.3.1-signed","type":"extension","internalName":null,"updateURL":null,"updateKey":null,"optionsURL":null,"optionsType":null,"aboutURL":null,"iconURL":"chrome://testpilot/skin/testpilot_32x32.png","icon64URL":null,"defaultLocale":{"name":"Test Pilot","description":"Help make Firefox better by running user studies.","creator":"Mozilla Corporation","homepageURL":"http://testpilot.mozillalabs.com/"},"visible":true,"active":true,"userDisabled":false,"appDisabled":false,"descriptor":"/Users/kiamlaluno/Library/Application Support/Firefox/Profiles/a9wsbury.default/extensions/testpilot@labs.mozilla.com.xpi","installDate":1431796864000,"updateDate":1432784122000,"applyBackgroundUpdates":1,"bootstrap":false,"size":1278820,"sourceURI":"https://addons.cdn.mozilla.net/user-media/addons/13661/test_pilot-1.2.3-fx.xpi?filehash=sha256%3A813e3080c355e98091fa321529cf3949739ef3a1497a4b45a3b5244aee443fac","releaseNotesURI":"https://addons.mozilla.org/versions/updateInfo/1501117/en-US/","softDisabled":false,"foreignInstall":false,"hasBinaryComponents":false,"strictCompatibility":false,"locales":[],"targetApplications":[{"id":"{ec8030f7-c20a-464f-9b0e-13a3a9e97384}","minVersion":"3.5","maxVersion":"17.0a1"}],"targetPlatforms":[],"multiprocessCompatible":false},
{"id":"dreditor@dreditor.org","syncGUID":"Lu-OEFQCHDuJ","location":"app-profile","version":"1.2.14","type":"extension","internalName":null,"updateURL":null,"updateKey":null,"optionsURL":null,"optionsType":null,"aboutURL":null,"iconURL":null,"icon64URL":null,"defaultLocale":{"name":"Dreditor","description":"An extension for Drupal.org that enhances user experience and functionality. Original author: Daniel F. Kudwien (sun).","creator":"Mark Carver","homepageURL":"https://dreditor.org"},"visible":true,"active":true,"userDisabled":false,"appDisabled":false,"descriptor":"/Users/kiamlaluno/Library/Application Support/Firefox/Profiles/a9wsbury.default/extensions/dreditor@dreditor.org.xpi","installDate":1432790689000,"updateDate":1432790689000,"applyBackgroundUpdates":1,"bootstrap":true,"size":124907,"sourceURI":"https://dreditor.org/dreditor.xpi?1.2.14","releaseNotesURI":null,"softDisabled":false,"foreignInstall":false,"hasBinaryComponents":false,"strictCompatibility":false,"locales":[],"targetApplications":[{"id":"{ec8030f7-c20a-464f-9b0e-13a3a9e97384}","minVersion":"21.0","maxVersion":"29.0a1"}],"targetPlatforms":[],"multiprocessCompatible":false},
{"id":"en-US@dictionaries.addons.mozilla.org","syncGUID":"n8s_OF7ZCuw1","location":"app-profile","version":"7.0.1","type":"dictionary","internalName":null,"updateURL":null,"updateKey":null,"optionsURL":null,"optionsType":null,"aboutURL":null,"iconURL":null,"icon64URL":null,"defaultLocale":{"name":"United States English Spellchecker","description":"English United States (en-US) Spellchecker","creator":"Giuliano Masseroni","homepageURL":"https://addons.mozilla.org/en-US/firefox/addon/3497","contributors":["Kevin Atkinson","Geoff Kuenning"]},"visible":true,"active":true,"userDisabled":false,"appDisabled":false,"descriptor":"/Users/kiamlaluno/Library/Application Support/Firefox/Profiles/a9wsbury.default/extensions/en-US@dictionaries.addons.mozilla.org","installDate":1433038761000,"updateDate":1433038761000,"applyBackgroundUpdates":1,"bootstrap":true,"size":701806,"sourceURI":"https://addons.mozilla.org/firefox/downloads/latest/3497/addon-3497-latest.xpi?src=dp-btn-primary","releaseNotesURI":null,"softDisabled":false,"foreignInstall":false,"hasBinaryComponents":false,"strictCompatibility":false,"locales":[],"targetApplications":[{"id":"{ec8030f7-c20a-464f-9b0e-13a3a9e97384}","minVersion":"17.0","maxVersion":"42.0"},{"id":"{3550f703-e582-4d05-9a08-453d09bdfdc6}","minVersion":"5.0","maxVersion":"23.0"},{"id":"{92650c4d-4b8e-4d2a-b7eb-24ecf4f6b63a}","minVersion":"2.0","maxVersion":"2.20"}],"targetPlatforms":[]},
{"id":"it-IT@dictionaries.addons.mozilla.org","syncGUID":"0HLJG3tK5S0H","location":"app-profile","version":"3.4.0","type":"dictionary","internalName":null,"updateURL":null,"updateKey":null,"optionsURL":null,"optionsType":null,"aboutURL":null,"iconURL":null,"icon64URL":null,"defaultLocale":{"name":"Italian dictionary","description":"Italian dictionary based on version 3.4.0 (2013-09-05) of the Italian dictionary created by project PLIO.","creator":"Francesco Lodolo","homepageURL":"http://www.mozillaitalia.org"},"visible":true,"active":true,"userDisabled":false,"appDisabled":false,"descriptor":"/Users/kiamlaluno/Library/Application Support/Firefox/Profiles/a9wsbury.default/extensions/it-IT@dictionaries.addons.mozilla.org","installDate":1433038989000,"updateDate":1433038989000,"applyBackgroundUpdates":1,"bootstrap":true,"size":1412342,"sourceURI":"https://addons.mozilla.org/firefox/downloads/latest/3053/platform:3/addon-3053-latest.xpi?src=dp-btn-primary","releaseNotesURI":null,"softDisabled":false,"foreignInstall":false,"hasBinaryComponents":false,"strictCompatibility":false,"locales":[{"locales":["it"],"name":"Dizionario italiano","description":"Dizionario italiano basato sulla versione 3.4.0 (2013-09-05) del dizionario realizzato dal progetto PLIO.","creator":null,"homepageURL":null}],"targetApplications":[{"id":"{ec8030f7-c20a-464f-9b0e-13a3a9e97384}","minVersion":"18.0a1","maxVersion":"41.0"},{"id":"{3550f703-e582-4d05-9a08-453d09bdfdc6}","minVersion":"18.0a1","maxVersion":"34.0"},{"id":"{92650c4d-4b8e-4d2a-b7eb-24ecf4f6b63a}","minVersion":"2.7a1","maxVersion":"2.31"},{"id":"{aa3c5121-dab2-40e2-81ca-7ea25febc110}","minVersion":"18.0a1","maxVersion":"34.0"},"id":"toolkit@mozilla.org","minVersion":"18.0a1","maxVersion":"34.0"}],"targetPlatforms":[]},
{"id":"onepassword4@agilebits.com","syncGUID":"fqAFQIuqP6rc","location":"app-profile","version":"4.4.2","type":"extension","internalName":null,"updateURL":"https://d13itkw33a7sus.cloudfront.net/dist/1P/ext/autoupdate_firefox4.xml","updateKey":null,"optionsURL":null,"optionsType":null,"aboutURL":null,"iconURL":null,"icon64URL":null,"defaultLocale":{"name":"1Password","description":"Password and identity manager for Mac, Windows, iOS and Android.","creator":"AgileBits Inc","homepageURL":"https://agilebits.com/onepassword"},"visible":true,"active":true,"userDisabled":false,"appDisabled":false,"descriptor":"/Users/kiamlaluno/Library/Application Support/Firefox/Profiles/a9wsbury.default/extensions/onepassword4@agilebits.com.xpi","installDate":1431759097000,"updateDate":1438291043000,"applyBackgroundUpdates":1,"bootstrap":true,"size":248521,"sourceURI":"https://d13itkw33a7sus.cloudfront.net/dist/1P/ext/1Password-4.4.2.xpi","releaseNotesURI":null,"softDisabled":false,"foreignInstall":false,"hasBinaryComponents":false,"strictCompatibility":false,"locales":[],"targetApplications":[{"id":"{ec8030f7-c20a-464f-9b0e-13a3a9e97384}","minVersion":"24.0","maxVersion":"27.0"}],"targetPlatforms":[],"multiprocessCompatible":false},
{"id":"langpack-en-US@firefox.mozilla.org","syncGUID":"hbdMIPDTjJjU","location":"app-profile","version":"39.0","type":"locale","internalName":null,"updateURL":null,"updateKey":null,"optionsURL":null,"optionsType":null,"aboutURL":null,"iconURL":null,"icon64URL":null,"defaultLocale":{"name":"English (US) Language Pack","description":null,"creator":"mozilla.org","homepageURL":null},"visible":true,"active":true,"userDisabled":false,"appDisabled":false,"descriptor":"/Users/kiamlaluno/Library/Application Support/Firefox/Profiles/a9wsbury.default/extensions/langpack-en-US@firefox.mozilla.org.xpi","installDate":1433038780000,"updateDate":1438297982000,"applyBackgroundUpdates":1,"bootstrap":true,"size":1107183,"sourceURI":"https://addons.cdn.mozilla.net/user-media/addons/407142/english_us_language_pack-39.0-fx.xpi?filehash=sha256%3Ae970523c118125330029522253119290be0c3fae810304b6f8851d5b1a08bb2a","releaseNotesURI":null,"softDisabled":false,"foreignInstall":false,"hasBinaryComponents":false,"strictCompatibility":true,"locales":[],"targetApplications":[{"id":"{ec8030f7-c20a-464f-9b0e-13a3a9e97384}","minVersion":"39.0","maxVersion":"39.*"}],"targetPlatforms":[]},
{"id":"{972ce4c6-7e08-4474-a285-3208198ce6fd}","syncGUID":"4aQXjLIYT8-y","location":"app-global","version":"39.0.3","type":"theme","internalName":"classic/1.0","updateURL":null,"updateKey":null,"optionsURL":null,"optionsType":null,"aboutURL":null,"iconURL":null,"icon64URL":null,"defaultLocale":{"name":"Default","description":"The default theme.","creator":"Mozilla","homepageURL":null,"contributors":["Mozilla Contributors"]},"visible":true,"active":true,"userDisabled":false,"appDisabled":false,"descriptor":"/Applications/Firefox.app/Contents/Resources/browser/extensions/{972ce4c6-7e08-4474-a285-3208198ce6fd}","installDate":1428098726000,"updateDate":1439001657000,"applyBackgroundUpdates":1,"skinnable":true,"size":3175,"sourceURI":null,"releaseNotesURI":null,"softDisabled":false,"foreignInstall":false,"hasBinaryComponents":false,"strictCompatibility":true,"locales":[],"targetApplications":[{"id":"{ec8030f7-c20a-464f-9b0e-13a3a9e97384}","minVersion":"39.0.3","maxVersion":"39.0.3"}],"targetPlatforms":[]},{"id":"{4ED1F68A-5463-4931-9384-8FFF5ED91D92}","syncGUID":"mtfF_fBR6ym-","location":"app-system-local","version":"2.0","type":"extension","internalName":null,"updateURL":null,"updateKey":null,"optionsURL":null,"optionsType":null,"aboutURL":null,"iconURL":null,"icon64URL":null,"defaultLocale":{"name":"McAfee SiteAdvisor","description":null,"creator":"McAfee Inc.","homepageURL":"http://www.siteadvisor.com/"},"visible":true,"active":true,"userDisabled":false,"appDisabled":false,"descriptor":"/Library/Application Support/Mozilla/Extensions/{ec8030f7-c20a-464f-9b0e-13a3a9e97384}/{4ED1F68A-5463-4931-9384-8FFF5ED91D92}","installDate":1417806531000,"updateDate":1417806531000,"applyBackgroundUpdates":1,"bootstrap":false,"size":18013,"sourceURI":null,"releaseNotesURI":null,"softDisabled":false,"foreignInstall":true,"hasBinaryComponents":false,"strictCompatibility":false,"locales":[],"targetApplications":[{"id":"{ec8030f7-c20a-464f-9b0e-13a3a9e97384}","minVersion":"4.0","maxVersion":"15.*"}],"targetPlatforms":[],"multiprocessCompatible":false}
]
}
我需要从{"id":
节中提取每条"addons"
行,并在"version":
和"defaultLocale"
节中获得"descriptor":
,“名称”: / p>
我只需要在awk
或sed
(基本上是任何标准linux命令或它们的组合)中进行相同的操作。我将无法使用jq
或任何其他工具来解析.json
文件。
请帮助
答案 0 :(得分:3)
jq
应该可以帮助您:
# some JSON resembling yours
JSON='{"schemaVersion":16, "addons":[{"id": "id_1"}, {"id": "id_2"}, {"id": "id_3"}]}'
jq -r '.addons[].id' <<< $JSON
给出:
id_1
id_2
id_3
答案 1 :(得分:1)
您的问题不是明确的要求,示例中的行太长,以至于无法试图找到您感兴趣的字符串的上下文,并且您没有向我们展示预期的输出,因此我们没有什么可以帮助的,但是这是一个起点,希望您可以适应做您想做的任何事情:
$ cat tst.awk
{
while ( match($0,/"(version|name)":("[^"]*"|[^,}]+)/) ) {
print NR, substr($0,RSTART,RLENGTH)
$0 = substr($0,RSTART+RLENGTH)
}
}
。
$ awk -f tst.awk file
4 "version":"1.2.3.1-signed"
4 "name":"Test Pilot"
5 "version":"1.2.14"
5 "name":"Dreditor"
6 "version":"7.0.1"
6 "name":"United States English Spellchecker"
7 "version":"3.4.0"
7 "name":"Italian dictionary"
7 "name":"Dizionario italiano"
8 "version":"4.4.2"
8 "name":"1Password"
9 "version":"39.0"
9 "name":"English (US) Language Pack"
10 "version":"39.0.3"
10 "name":"Default"
10 "version":"2.0"
10 "name":"McAfee SiteAdvisor"
。
$ cat tst.awk
{
while ( match($0,/"defaultLocale":{[^{}]+}|"descriptor":("[^"]*"|[^,}]+)/) ) {
sect = substr($0,RSTART,RLENGTH)
$0 = substr($0,RSTART+RLENGTH)
print NR, sect
while ( match(sect,/"(version|name)":("[^"]*"|[^,}]+)/) ) {
print "\t" substr(sect,RSTART,RLENGTH)
sect = substr(sect,RSTART+RLENGTH)
}
}
print ""
}
。
$ awk -f tst.awk file
4 "defaultLocale":{"name":"Test Pilot","description":"Help make Firefox better by running user studies.","creator":"Mozilla Corporation","homepageURL":"http://testpilot.mozillalabs.com/"}
"name":"Test Pilot"
4 "descriptor":"/Users/kiamlaluno/Library/Application Support/Firefox/Profiles/a9wsbury.default/extensions/testpilot@labs.mozilla.com.xpi"
5 "defaultLocale":{"name":"Dreditor","description":"An extension for Drupal.org that enhances user experience and functionality. Original author: Daniel F. Kudwien (sun).","creator":"Mark Carver","homepageURL":"https://dreditor.org"}
"name":"Dreditor"
5 "descriptor":"/Users/kiamlaluno/Library/Application Support/Firefox/Profiles/a9wsbury.default/extensions/dreditor@dreditor.org.xpi"
6 "defaultLocale":{"name":"United States English Spellchecker","description":"English United States (en-US) Spellchecker","creator":"Giuliano Masseroni","homepageURL":"https://addons.mozilla.org/en-US/firefox/addon/3497","contributors":["Kevin Atkinson","Geoff Kuenning"]}
"name":"United States English Spellchecker"
6 "descriptor":"/Users/kiamlaluno/Library/Application Support/Firefox/Profiles/a9wsbury.default/extensions/en-US@dictionaries.addons.mozilla.org"
7 "defaultLocale":{"name":"Italian dictionary","description":"Italian dictionary based on version 3.4.0 (2013-09-05) of the Italian dictionary created by project PLIO.","creator":"Francesco Lodolo","homepageURL":"http://www.mozillaitalia.org"}
"name":"Italian dictionary"
7 "descriptor":"/Users/kiamlaluno/Library/Application Support/Firefox/Profiles/a9wsbury.default/extensions/it-IT@dictionaries.addons.mozilla.org"
8 "defaultLocale":{"name":"1Password","description":"Password and identity manager for Mac, Windows, iOS and Android.","creator":"AgileBits Inc","homepageURL":"https://agilebits.com/onepassword"}
"name":"1Password"
8 "descriptor":"/Users/kiamlaluno/Library/Application Support/Firefox/Profiles/a9wsbury.default/extensions/onepassword4@agilebits.com.xpi"
9 "defaultLocale":{"name":"English (US) Language Pack","description":null,"creator":"mozilla.org","homepageURL":null}
"name":"English (US) Language Pack"
9 "descriptor":"/Users/kiamlaluno/Library/Application Support/Firefox/Profiles/a9wsbury.default/extensions/langpack-en-US@firefox.mozilla.org.xpi"
10 "defaultLocale":{"name":"Default","description":"The default theme.","creator":"Mozilla","homepageURL":null,"contributors":["Mozilla Contributors"]}
"name":"Default"
10 "descriptor":"/Applications/Firefox.app/Contents/Resources/browser/extensions/{972ce4c6-7e08-4474-a285-3208198ce6fd}"
10 "defaultLocale":{"name":"McAfee SiteAdvisor","description":null,"creator":"McAfee Inc.","homepageURL":"http://www.siteadvisor.com/"}
"name":"McAfee SiteAdvisor"
10 "descriptor":"/Library/Application Support/Mozilla/Extensions/{ec8030f7-c20a-464f-9b0e-13a3a9e97384}/{4ED1F68A-5463-4931-9384-8FFF5ED91D92}"
以上内容可在任何UNIX盒上的任何shell中使用任何awk进行工作。请注意,它仅与您发布的示例输入相同,不要指望它通常对于JSON都能正常运行。
答案 2 :(得分:0)
如果要解析更复杂的json
,也许应该考虑使用专门用于jq
处理的工具(如注释中建议的json
。)
但是,如果您的json
与上述格式完全相同,则可以使用sed
将其过滤为普通文本文件,如下所示。
sed '0,/"id"/ d; /}/,/"id"/ d;s/\s*//' input.txt
输出:
//data 1 # suffixed with a number to indicate different lines
//data 2
//data 3
//data 4