Linux中shell腳本的jq命令用法詳解
1、jq介紹
jq是Linux系統(tǒng)中把文本字符串格式化成json格式的工具。
jq命令用于將JSON數(shù)據(jù)轉(zhuǎn)換為更易讀的格式并將其打印到 Linux 上的標(biāo)準(zhǔn)輸出。
jq命令是圍繞過(guò)濾器構(gòu)建的,過(guò)濾器用于從JSON文件中僅查找和打印所需的數(shù)據(jù)。
jq是一個(gè)輕量級(jí)的命令行JSON處理工具,用于解析、過(guò)濾、修改和操作JSON數(shù)據(jù)。
它提供了一種簡(jiǎn)潔和靈活的方式來(lái)處理JSON數(shù)據(jù),可以與其他命令行工具(如grep、sed)結(jié)合使用,以實(shí)現(xiàn)復(fù)雜的JSON處理任務(wù)。
2、jq安裝
安裝EPEL源(這一步可以省略): yum install epel-release -y 安裝完EPEL源后,可以查看下jq包是否存在 yum list jq 安裝jq yum install jq -y
3、json數(shù)據(jù)準(zhǔn)備
[{ "id": "1", "name": "zhangsan", "age": "18" }, { "id": "2", "name": "lisi", "age": "19" }, { "id": "3", "name": "wangwu", "age": "20" }]
4、jq使用
4.1 jq --help
[root@dgw-machine ~]# jq --help jq - commandline JSON processor [version 1.6] Usage: jq [options] <jq filter> [file...] jq [options] --args <jq filter> [strings...] jq [options] --jsonargs <jq filter> [JSON_TEXTS...] jq is a tool for processing JSON inputs, applying the given filter to its JSON text inputs and producing the filter's results as JSON on standard output. The simplest filter is ., which copies jq's input to its output unmodified (except for formatting, but note that IEEE754 is used for number representation internally, with all that that implies). For more advanced filters see the jq(1) manpage ("man jq") and/or https://stedolan.github.io/jq Example: $ echo '{"foo": 0}' | jq . { "foo": 0 } Some of the options include: -c compact instead of pretty-printed output; -n use `null` as the single input value; -e set the exit status code based on the output; -s read (slurp) all inputs into an array; apply filter to it; -r output raw strings, not JSON texts; -R read raw strings, not JSON texts; -C colorize JSON; -M monochrome (don't colorize JSON); -S sort keys of objects on output; --tab use tabs for indentation; --arg a v set variable $a to value <v>; --argjson a v set variable $a to JSON value <v>; --slurpfile a f set variable $a to an array of JSON texts read from <f>; --rawfile a f set variable $a to a string consisting of the contents of <f>; --args remaining arguments are string arguments, not files; --jsonargs remaining arguments are JSON arguments, not files; -- terminates argument processing; Named arguments are also available as $ARGS.named[], while positional arguments are available as $ARGS.positional[]. See the manpage for more options.
4.2 顯示數(shù)據(jù)
jq '.' test.json 或者 cat test.json | jq '.' cat test.json | jq -r '.'
4.3 訪問(wèn)和輸出 JSON 文件中數(shù)組中存在的元素
jq '.[]' test.json
4.4 過(guò)濾數(shù)組
可以使用方括號(hào)([])來(lái)過(guò)濾數(shù)組中的元素。例如,要選擇數(shù)組中的第一個(gè)元素,可以使用以下命令:
jq '.[1]' test.json
4.5 訪問(wèn)屬性
使用點(diǎn)操作符(.)來(lái)選擇JSON對(duì)象中的字段。
例如,要選擇名為"name"的字段,可以使用以下命令:
jq '.[1].name' test.json
4.6 過(guò)濾條件(if-then-else)
jq 'if .[].age > "19" then .[].name else empty end' test.json
4.7 過(guò)濾多個(gè)字段
可以使用逗號(hào)(,)將多個(gè)字段組合在一起。
例如,要選擇名字和年齡字段,可以使用以下命令:
jq '.[].name, .[].age' test.json
4.8 過(guò)濾組合操作符(and/or)
可以使用邏輯操作符(and/or)來(lái)組合多個(gè)過(guò)濾條件。例如,要選擇年齡大于18歲并且名字以"J"開頭的人,可以使用以下命令:
jq 'select(.age > 18 and .name | startswith("J"))' file.json
4.9 修改字段
可以使用賦值操作符(=)來(lái)修改字段的值。例如,要將名字字段修改為"John",可以使用以下命令:
jq '.[1].name = "John"' test.json
4.10 過(guò)濾數(shù)組中的對(duì)象
可以使用點(diǎn)操作符(.)和方括號(hào)([])來(lái)選擇數(shù)組中的對(duì)象。例如,要選擇數(shù)組中所有年齡大于19歲的人,可以使用以下命令:
jq '.[] | select(.age > "19")' test.json
4.11 過(guò)濾嵌套字段
可以使用點(diǎn)操作符(.)來(lái)選擇嵌套在對(duì)象中的字段。
例如,要選擇嵌套在"address"字段中的"city"字段,可以使用以下命令:
jq '.address.city' file.json
4.12數(shù)組聚合操作
可以使用reduce
函數(shù)將數(shù)組中的元素聚合成一個(gè)值。
例如,要計(jì)算數(shù)組中年齡的總和,可以使用以下命令:
jq 'reduce .[] as $item (0; . + $item.age)' file.json
4.13數(shù)組映射操作
可以使用map
函數(shù)將數(shù)組中的元素進(jìn)行映射轉(zhuǎn)換。
例如,要將數(shù)組中的所有年齡加1,可以使用以下命令
jq 'map(.age + 1)' file.json
4.14數(shù)組排序
可以使用sort_by
函數(shù)對(duì)數(shù)組進(jìn)行排序。
例如,要按照年齡對(duì)數(shù)組中的對(duì)象進(jìn)行升序排序,可以使用以下命令:
jq 'sort_by(.age)' file.json
4.15數(shù)組切片
可以使用切片操作符([start:end])來(lái)選擇數(shù)組中的一部分元素。
例如,要選擇數(shù)組中的前3個(gè)元素,可以使用以下命令:
jq '.[:3]' file.json
4.16變量和函數(shù)
可以使用$
符號(hào)定義變量,并使用def
關(guān)鍵字定義函數(shù)。
例如,要定義一個(gè)函數(shù)來(lái)計(jì)算人的BMI指數(shù),可以使用以下命令:
jq 'def bmi: .weight / (.height * .height); .[] | .name, bmi' file.json
到此這篇關(guān)于Linux中shell腳本的jq命令用法詳解的文章就介紹到這了,更多相關(guān)shell腳本的jq命令內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Shell腳本實(shí)現(xiàn)溫和方式重啟Centos系統(tǒng)
這篇文章主要介紹了Shell腳本實(shí)現(xiàn)溫和方式重啟Centos系統(tǒng),本文腳本主要目的是用于重啟后臺(tái)比較重要的進(jìn)程,需要的朋友可以參考下2014-12-12Shell腳本實(shí)現(xiàn)檢查服務(wù)器安全狀態(tài)(用戶、登錄IP、防火墻檢查)
這篇文章主要介紹了Shell腳本實(shí)現(xiàn)檢查服務(wù)器安全狀態(tài),本文主要檢查3個(gè)方面,分別是系統(tǒng)用戶檢查、登錄IP檢查、防火墻狀態(tài)檢查,需要的朋友可以參考下2014-12-12linux下mysql如何自動(dòng)備份shell腳本
對(duì)任何一個(gè)已經(jīng)上線的網(wǎng)站站點(diǎn)來(lái)說(shuō),數(shù)據(jù)備份都是必須的。無(wú)論版本更新還是服務(wù)器遷移,備份數(shù)據(jù)的重要性不言而喻。人工備份數(shù)據(jù)的方式不單耗費(fèi)大量時(shí)間和精力,還灰常不專業(yè)。下面小編給大家分享linux下mysql自動(dòng)備份shell腳本,需要的朋友可以參考下2015-09-09shell進(jìn)度條追蹤指令執(zhí)行時(shí)間的場(chǎng)景分析
這篇文章主要介紹了shell進(jìn)度條如何追蹤指令執(zhí)行時(shí)間,本文中的進(jìn)度條可以應(yīng)用于大部分場(chǎng)景。不用修改進(jìn)度條函數(shù)的任何代碼,就可以直接使用,特別適合那些可預(yù)估指令執(zhí)行時(shí)間的場(chǎng)景,需要的朋友可以參考下2022-06-06用Shell腳本快速搭建Ubuntu下的Nodejs開發(fā)環(huán)境
這篇文章主要介紹了用Shell腳本快速搭建Ubuntu下的Nodejs開發(fā)環(huán)境的方法,需要的朋友可以參考下2014-03-03