[SOLVED] sed not working as expect while parsing json
Linux - ServerThis forum is for the discussion of Linux Software used in a server related context.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hello, trying to parse json using sed, just to get a version number. The script is simple, but for whatever reason I get more text than I would expect.
Here is the code:
Code:
#!/bin/sh -e
api_url="https://api.bintray.com"
get_latest_version() {
local version=0
local tmpfile="/tmp/.version"
rc=$(curl -sk "${api_url}/search/packages?name=jfrog-artifactory-pro&subject=jfrog&repo=artifactory-pro-rpms" -o $tmpfile -w '%{http_code}')
if [ $rc -eq 200 ]; then
# "latest_version":"5.10.2"
version=$(sed -n 's/.*\"\(latest_version\)\":\"\(.*\)\"\,/\2/p' $tmpfile 2>/dev/null)
fi
rm -f $tmpfile
echo $version
}
echo $(get_latest_version)
I know I'm doing something wrong, but can't figure out what. Any ideas are welcomed!
The language sed would be the wrong tool for that. You'll benefit from a proper JSON parser.
You can have a standalone like jq or use one for the scripting language of your choice. For example there is CPAN's JSON for perl.
Thats not an option when running in a small environment without those tools, sed should be able to handle this, just I'm not seeing what I'm doing wrong.
perl is in all environments, unless you've assembled something really custom. jq can be added to any. JSON, however, can be really messy from a whitespace perspective, and that includes linebreaks, which makes it hard to use something simple like sed.
But if you are dead set on (mis-)using sed in that, then please post a line from the sample data to be worked on and what the line should look like after the transformation. It's a bit hard to guess without seeing a before and after sample of the data.
perl is in all environments, unless you've assembled something really custom. jq can be added to any. JSON, however, can be really messy from a whitespace perspective, and that includes linebreaks, which makes it hard to use something simple like sed.
But if you are dead set on (mis-)using sed in that, then please post a line from the sample data to be worked on and what the line should look like after the transformation. It's a bit hard to guess without seeing a before and after sample of the data.
This is the exact output:
Code:
robert@pandora:~> curl -sk -X GET "https://api.bintray.com/search/packages?name=jfrog-artifactory-pro&subject=jfrog&repo=artifactory-pro-rpms"
[{"name":"jfrog-artifactory-pro-rpm","repo":"artifactory-pro-rpms","owner":"jfrog","desc":"Artifactory is an advanced Binary Repository Manager for use by build tools (like Maven and Gradle), dependency management tools (like Ivy and NuGet) and build servers (like Jenkins, Hudson, TeamCity and Bamboo).\r\nRepository managers serve two purposes: they act as highly configurable proxies between your organization and external repositories and they also provide build servers with a deployment destination for your internally generated artifacts.","labels":["artifactory","powerpack"],"attribute_names":[],"licenses":[],"custom_licenses":["JFrog Artifactory EULA"],"followers_count":16,"created":"2015-05-21T13:52:20.704Z","website_url":"http://www.jfrog.com/","issue_tracker_url":"https://issues.jfrog.org/jira/browse/rtfact","linked_to_repos":[],"permissions":[],"versions":["5.10.2","5.10.1","5.10.0","5.9.3","5.9.1","5.9.0","5.8.4","5.8.3","5.8.2","5.8.1","5.8.0","5.7.2","5.7.1","5.7.0","5.6.3","5.6.2","5.6.1","5.6.0","5.5.2","5.5.1","5.5.0","5.4.6","5.4.5","5.4.4","5.4.3","5.4.2","5.4.1","5.4.0","5.3.2","5.3.1","5.3.0","5.2.1","5.2.0","5.1.4","5.1.3","5.1.0","5.0.1","5.0.0","4.16.1","4.16.0","4.15.0","4.14.3","4.14.2","4.14.1","4.14.0","4.13.2","4.13.1","4.13.0","4.12.2","4.12.1","4.12.0.1","4.11.2","4.11.1","4.11.0","4.10.0","4.9.1","4.9.0","4.8.2","4.8.1","4.8.0","4.7.7","4.7.6","4.7.5","4.7.4","4.7.3","4.7.2","4.7.1","4.7.0","4.6.1","4.6.0","4.5.2","4.5.1","4.5.0","4.4.3","4.4.2","4.4.1","4.4.0","4.3.3","4.3.2","4.3.1","4.3.0","4.2.2","4.2.1","4.2.0","4.1.3","4.1.2","4.1.0","4.0.2","4.0.1","4.0.0","3.9.5","3.9.4","3.9.3","3.9.2","3.9.1","3.9.0","3.8.0","3.7.0","3.6.0","3.5.3","3.5.2.1","3.5.2","3.5.1","3.5.0","2.4.0","3.0.0","2.3.3","3.0.3","2.5.0","3.1.0","3.2.0","2.6.0","3.3.0","2.3.3.1","2.3.4.1","2.6.3","3.5.2.1a","3.5.3b","3.4.0","3.6.0a","3.3.0.1","2.5.2","2.6.6","3.6.0-beta-1","3.2.2","3.5.2.1b","3.5.1d","3.0.1","2.5.1","2.6.5","3.1.1.1","3.4.2","3.2.1","3.3.1","3.5.3a","3.4.1","2.6.4","3.2.1.1","2.6.7","2.4.2","3.0.2","2.6.2","2.4.1","3.1.1","2.3.4","2.6.7.1","2.5.1.1","3.0.4","2.6.1"],"latest_version":"5.10.2","updated":"2018-04-12T18:43:35.725Z","rating_count":0,"system_ids":[],"vcs_url":"http://bit.ly/1U94K5i","maturity":""}]
Thanks. It looks like you might be missing a [ in your pattern, that's a hard one so it might be best to just check for not a quote. And if you are just substituting out all the version numbers for the one single version number then you'll want a span of all characters except a quote. [^"]
What do you want that line to look like when you are done processing it? Just the one version by itself?
Code:
sed -n -r 's/^.*(latest_version)"[^"]*"([^"]*)",.*$/\2/p'
Last edited by Turbocapitalist; 04-13-2018 at 02:26 PM.
Thanks. It looks like you might be missing a [ in your pattern, that's a hard one so it might be best to just check for not a quote. And if you are just substituting out all the version numbers for the one single version number then you'll want a span of all characters except a quote. [^"]
What do you want that line to look like when you are done processing it? Just the one version by itself?
Code:
sed -n -r 's/^.*(latest_version)"[^"]*"([^"]*)",.*$/\2/p'
you can use shell/sed/grep/awk/perl/python, yes, all of them are ok, but the regular way would be either jq or perl/python (or similar) with a json module. Otherwise the result will not be fully reliable.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.