2017-03-08 87 views
1

我有一個文件的abc.txt的內容是如何從一個文本文件

{ 「storageSummary」 打印特定的詞:{ 「binariesSummary」:{ 「binariesCount」: 「703」, 「binariesSize」:」 882.39 MB「,」artifactsSize「:」3.41 GB「,」優化「:」25.23%「,」itemsCount「:」4,126「,」artifactsCount「:」1,917「},」fileStoreSummary「:{」storageType「:」file - 「system」,「storageDirectory」:「/ jfrog_uat_nfs/binaries」,「totalSpace」:「1.30 TB」,「usedSpace」:「1.23 GB(0.09%)」,「freeSpace」:「1.30 TB(99.91%)」}} ,「repositoriesSummaryList」:[{「repoKey」:「sbt_remote-cache」,「repoType」:「CACHE」,「foldersCount」:0,「filesCount」:0,「usedSpace」:「0字節」,「itemsCount」 0 「packageType」: 「SBT」, 「百分比」: 「0%」},{ 「repoKey」: 「test7.mvlo」, 「repoType」: 「本地」, 「foldersCount」:0 「filesCount」:1 ,「usedSpace」:「128字節」,「itemsCount」:1,「packageType」:「Maven」,「百分比」:「0%」},{「repoKey」 :「scripttestkp.rplo」,「repoType」:「LOCAL」,「foldersCount」:0,「filesCount」:0,「usedSpace」:「0字節」,「itemsCount」:0,「packageType」:「RPM」, 「percentage」:「0%」},{「repoKey」:「test7.grvr」,「repoType」:「VIRTUAL」,「foldersCount」:0,「filesCount」:0,「usedSpace」:「0字節」, 「itemsCount」:0,「packageType」:「Gradle」,「百分比」

從該文件中如何在shell腳本中只打印「repoKey」和「usedSpace」。

+1

這是一個'json'輸入?看起來像一個破'JSON'輸入標題上進行驗證文件,並提供完整的'json'輸入。 – Inian

+0

修復輸入文件後使用適當的'JSON'解析器 – Inian

+0

爲什麼說json是錯誤的,英語?看起來沒問題乍一看...看起來他錯過了一些剪切和粘貼也許 –

回答

-1

你需要訪問:data.storageSummary.repositoriesSummaryList [I] .repoKey每個我在列表

除了在迭代有usedSpace作爲重點...例如一切:data.storageSummary .fileStoreSummary.usedSpace和data.storageSummary.repositoriesSummaryList [I] .usedSpace對於每個i中的Linux機器命令行使用JQ

,該命令訪問usedSpace的第一個實例是:

curl "site" | jq ".storageSummary.fileStoreSummary.usedSpace" 

輸出是:

"1.23 GB (0.09%)" 

訪問的值的陣列,用於repoKeys下usedSpace命令是:

curl "site" | jq ".storageSummary.repositoriesSummaryList[].usedSpace" 

輸出爲:

"0 bytes" 
"128 bytes" 
"0 bytes" 
"0 bytes" 

的命令訪問的repoKey是值的數組:

curl "site" | jq ".storageSummary.repositoriesSummaryList[].repoKey" 

ou tput的是:

"sbt_remote-cache" 
"test7.mvlo" 
"scripttestkp.rplo" 
"test7.grvr" 

這可以通過修正提供的JSON,使其有效的,那麼到https://jqplay.org/