парсинг файла json с помощью awk или sed путем разделения содержимого на строки в зависимости от начала и конца - PullRequest
0 голосов
/ 03 мая 2019

У меня есть .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":.

Мне нужно сделать то же самое только в awk или sed (в основном любые стандартные команды linux или их комбинации). Я не смогу использовать jq или любые другие инструменты для анализа файла .json.

Пожалуйста, помогите

Ответы [ 3 ]

3 голосов
/ 03 мая 2019

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 голос
/ 04 мая 2019

Ваш вопрос не ясен в отношении требований, строки в примере слишком длинны, чтобы разбираться в попытках найти контекст для интересующих вас строк, и вы не показали нам ожидаемый результат, так что есть мы мало что можем сделать, чтобы помочь, но вот начало, которое, мы надеемся, можно адаптировать, чтобы делать то, что вы пытаетесь сделать:

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

Вышеописанное будет работать с использованием любого awk в любой оболочке на любом компьютере UNIX. Обратите внимание, что он делает то же, что и с опубликованными примерами ввода, не ожидайте, что он будет работать надежно для JSON в целом.

0 голосов
/ 03 мая 2019

Возможно, вам следует рассмотреть возможность использования инструмента, специализированного для обработки 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
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...