Linux中shell腳本的jq命令用法詳解
1、jq介紹
jq是Linux系統(tǒng)中把文本字符串格式化成json格式的工具。
jq命令用于將JSON數(shù)據(jù)轉換為更易讀的格式并將其打印到 Linux 上的標準輸出。
jq命令是圍繞過濾器構建的,過濾器用于從JSON文件中僅查找和打印所需的數(shù)據(jù)。
jq是一個輕量級的命令行JSON處理工具,用于解析、過濾、修改和操作JSON數(shù)據(jù)。
它提供了一種簡潔和靈活的方式來處理JSON數(shù)據(jù),可以與其他命令行工具(如grep、sed)結合使用,以實現(xiàn)復雜的JSON處理任務。
2、jq安裝
安裝EPEL源(這一步可以省略): yum install epel-release -y 安裝完EPEL源后,可以查看下jq包是否存在 yum list jq 安裝jq yum install jq -y
3、json數(shù)據(jù)準備
[{ "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 訪問和輸出 JSON 文件中數(shù)組中存在的元素
jq '.[]' test.json
4.4 過濾數(shù)組
可以使用方括號([])來過濾數(shù)組中的元素。例如,要選擇數(shù)組中的第一個元素,可以使用以下命令:
jq '.[1]' test.json
4.5 訪問屬性
使用點操作符(.)來選擇JSON對象中的字段。
例如,要選擇名為"name"的字段,可以使用以下命令:
jq '.[1].name' test.json
4.6 過濾條件(if-then-else)
jq 'if .[].age > "19" then .[].name else empty end' test.json
4.7 過濾多個字段
可以使用逗號(,)將多個字段組合在一起。
例如,要選擇名字和年齡字段,可以使用以下命令:
jq '.[].name, .[].age' test.json
4.8 過濾組合操作符(and/or)
可以使用邏輯操作符(and/or)來組合多個過濾條件。例如,要選擇年齡大于18歲并且名字以"J"開頭的人,可以使用以下命令:
jq 'select(.age > 18 and .name | startswith("J"))' file.json
4.9 修改字段
可以使用賦值操作符(=)來修改字段的值。例如,要將名字字段修改為"John",可以使用以下命令:
jq '.[1].name = "John"' test.json
4.10 過濾數(shù)組中的對象
可以使用點操作符(.)和方括號([])來選擇數(shù)組中的對象。例如,要選擇數(shù)組中所有年齡大于19歲的人,可以使用以下命令:
jq '.[] | select(.age > "19")' test.json
4.11 過濾嵌套字段
可以使用點操作符(.)來選擇嵌套在對象中的字段。
例如,要選擇嵌套在"address"字段中的"city"字段,可以使用以下命令:
jq '.address.city' file.json
4.12數(shù)組聚合操作
可以使用reduce
函數(shù)將數(shù)組中的元素聚合成一個值。
例如,要計算數(shù)組中年齡的總和,可以使用以下命令:
jq 'reduce .[] as $item (0; . + $item.age)' file.json
4.13數(shù)組映射操作
可以使用map
函數(shù)將數(shù)組中的元素進行映射轉換。
例如,要將數(shù)組中的所有年齡加1,可以使用以下命令
jq 'map(.age + 1)' file.json
4.14數(shù)組排序
可以使用sort_by
函數(shù)對數(shù)組進行排序。
例如,要按照年齡對數(shù)組中的對象進行升序排序,可以使用以下命令:
jq 'sort_by(.age)' file.json
4.15數(shù)組切片
可以使用切片操作符([start:end])來選擇數(shù)組中的一部分元素。
例如,要選擇數(shù)組中的前3個元素,可以使用以下命令:
jq '.[:3]' file.json
4.16變量和函數(shù)
可以使用$
符號定義變量,并使用def
關鍵字定義函數(shù)。
例如,要定義一個函數(shù)來計算人的BMI指數(shù),可以使用以下命令:
jq 'def bmi: .weight / (.height * .height); .[] | .name, bmi' file.json
到此這篇關于Linux中shell腳本的jq命令用法詳解的文章就介紹到這了,更多相關shell腳本的jq命令內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Shell腳本實現(xiàn)溫和方式重啟Centos系統(tǒng)
這篇文章主要介紹了Shell腳本實現(xiàn)溫和方式重啟Centos系統(tǒng),本文腳本主要目的是用于重啟后臺比較重要的進程,需要的朋友可以參考下2014-12-12Shell腳本實現(xiàn)檢查服務器安全狀態(tài)(用戶、登錄IP、防火墻檢查)
這篇文章主要介紹了Shell腳本實現(xiàn)檢查服務器安全狀態(tài),本文主要檢查3個方面,分別是系統(tǒng)用戶檢查、登錄IP檢查、防火墻狀態(tài)檢查,需要的朋友可以參考下2014-12-12用Shell腳本快速搭建Ubuntu下的Nodejs開發(fā)環(huán)境
這篇文章主要介紹了用Shell腳本快速搭建Ubuntu下的Nodejs開發(fā)環(huán)境的方法,需要的朋友可以參考下2014-03-03