Bash Sed/Tr对api输出进行排序
我需要帮助排序api输出和排序,并获得最新的Bash Sed/Tr对api输出进行排序,bash,shell,unix,sed,tr,Bash,Shell,Unix,Sed,Tr,我需要帮助排序api输出和排序,并获得最新的 { "name" : "xyz.abc", "tags" : [ "1.0.33", "1.0.35", "1.0.47", "1.0.63", "1.0.56", "1.0.45", "1.0.58", "1.0.31", &q
{
"name" : "xyz.abc",
"tags" : [ "1.0.33", "1.0.35", "1.0.47", "1.0.63", "1.0.56", "1.0.45", "1.0.58", "1.0.31", "1.0.39",
"1.0.30", "1.0.51", "1.0.41", "1.0.46", "1.0.32", "1.0.64", "1.0.65", "1.0.67", "1.0.42", "1.0.36",
"1.0.37", "1.0.53", "1.0.43", "1.0.44", "1.0.48", "1.0.49" ]
}
我试过了-
cat test2.text | grep tags | tr -d '[|]' | sed -n '/ *"tags" *: *"/ { s///; s/".*//; p; }'`
1.0.33
所需产出:
1.0.67
试试这个:
cat test2.text | jq .tags | tr -d '"|,|[|]' | awk '{print $1}' | sort | tail -1
输出:
1.0.67
您可以将
ruby
与内置JSON库一起使用,如下所示:
$ ruby -r json -e 'puts JSON.parse($<.read)["tags"]' file | sort | tail -n 1
1.0.67
或者,使用jq
和sed
:
$ jq -c '.tags | sort' file | sed -nE 's/.*"([0-9.]*)"]$/\1/p'
1.0.67
或者只需使用jqmax:
$ jq -c '.tags | max' file
"1.0.67" # you can remove the " with sed
这些方法适用于您的示例,但对于其他常见的版本字符串将失败。例如,1.0.255
是比1.0.67
更高或更早的版本吗
如果您想进行适当的版本排序(例如1.0.255
排序晚于1.0.67
),您可以在Ruby中使用以下工具进行排序:
$ ruby -r json -e 'puts JSON.parse($<.read)["tags"].sort_by{|s| Gem::Version.new(s)}[-1]' file
$ruby-rjson-e'放置json.parse($使用理解json(jq)和版本(GNU排序)的工具
如果要解析JSON,请使用专门用于此目的的工具:jq
。并非所有标记值都与“标记”
在tr-d'[|]中的
的目的是什么“
?该字符没有出现在API输出中,为什么需要删除它?我认为他的目标是删除除数字以外的所有字符以尝试对其进行排序。
$ ruby -r json -e 'puts JSON.parse($<.read)["tags"].sort_by{|s| Gem::Version.new(s)}[-1]' file
jq -r .tags[] test2.text | sort -V | tail -1