LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Retrieve the content of a json file using shell script .Without jq /json (https://www.linuxquestions.org/questions/linux-newbie-8/retrieve-the-content-of-a-json-file-using-shell-script-without-jq-json-4175671935/)

Historia 03-25-2020 06:57 AM

Retrieve the content of a json file using shell script .Without jq /json
 
I need to get the read contents of this json file
{
"instance": {
"id": "i-061a20e8c5",
"taglist" : [
{"Key": "acp-ims-backup","Value": "sqldb"} ,
{"Key": "created by","Value": "Historia"}
],
"backup_config_clientname": "CAJ-i-061a20e8c"
}
}

I want the value of "backup_config_clientname"
I can assign it to a variable and print it.
using awk or sed .

Thanks in advance

shruggy 03-25-2020 07:26 AM

And what's wrong with jq that you don't want to use it? I mean grep, sed, awk and friends are best suited for manipulating line-oriented data. They are an exceptionally bad fit for parsing non-line oriented formats like JSON or HTML.

syg00 03-25-2020 07:28 AM

More to the point - show us what efforts you have made yourself; either jq, awk, sed, ... whatever.

Historia 03-25-2020 07:36 AM

I have tried using jq
Well the problem in redhat is just a yum (yum install jq ) command and for suse I have to hit the internet, which I would like to avoid .
I saw these are the commands
zypper addrepo https://download.opensuse.org/reposi...SUSE:12.3.repo
zypper refresh
zypper install json-c

Historia 03-25-2020 07:37 AM

Quote:

Originally Posted by shruggy (Post 6104160)
And what's wrong with jq that you don't want to use it? I mean grep, sed, awk and friends are best suited for manipulating line-oriented data. They are an exceptionally bad fit for parsing non-line oriented formats like JSON or HTML.

I have tried using jq
Well the problem in redhat is just a yum (yum install jq ) command and for suse I have to hit the internet, which I would like to avoid .
I saw these are the commands
zypper addrepo https://download.opensuse.org/reposi...SUSE:12.3.repo
zypper refresh
zypper install json-c

Historia 03-25-2020 07:38 AM

Quote:

Originally Posted by syg00 (Post 6104161)
More to the point - show us what efforts you have made yourself; either jq, awk, sed, ... whatever.

clientname=`jq -r '.instance.backup_config_clientname' $configfile`

echo "Backup Clinet name $clientname"

configfile is the name of the file

TB0ne 03-25-2020 07:50 AM

Quote:

Originally Posted by Historia (Post 6104170)
I have tried using jq
Well the problem in redhat is just a yum (yum install jq ) command and for suse I have to hit the internet, which I would like to avoid .
I saw these are the commands
zypper addrepo https://download.opensuse.org/reposi...SUSE:12.3.repo
zypper refresh
zypper install json-c

What version/distro of Linux are you using?? Because the repository you mention is VERY old. And why are you wanting to avoid hitting the Internet to install a package? Why is that an issue? Pretty much all distros use online repositories. Also seems fairly easy (providing your input file is consistent), to just grep for "backup_config_clientname", then use awk/sed to tidy it up.

shruggy 03-25-2020 07:53 AM

Quote:

Originally Posted by Historia (Post 6104170)
for suse I have to hit the internet

Sorry, I don't get it. According to pkgs.org jq is present in the OpenSUSE Oss repository.

Historia 03-25-2020 08:14 AM

Quote:

Originally Posted by TB0ne (Post 6104178)
What version/distro of Linux are you using?? Because the repository you mention is VERY old. And why are you wanting to avoid hitting the Internet to install a package? Why is that an issue? Pretty much all distros use online repositories. Also seems fairly easy (providing your input file is consistent), to just grep for "backup_config_clientname", then use awk/sed to tidy it up.



I am using it for a suse 12 machine. Probably for all Suse machines.
I am not good at awk and sed , that's why posted the question :(

pan64 03-25-2020 08:24 AM

believe me, much easier to install jq and use it. Especially because you posted the solution in #6.

Code:

grep -P -o '(?<="backup_config_clientname": ")([^"]*)'
sed -n '/backup_config_client/{s/^.*: "//;s/".*$//;p}'


Historia 03-25-2020 08:26 AM

Quote:

Originally Posted by pan64 (Post 6104188)
believe me, much easier to install jq and use it. Especially because you posted the solution in #6.

Code:

grep -P -o '(?<="backup_config_clientname": ")([^"]*)'


Marking this as SOLVED

shruggy 03-25-2020 08:30 AM

As a quick and dirty workaround
Code:

eval clientname="$(awk -F': *' '/backup_config_clientname/,$0=$2' $configfile)"
echo "Backup client name $clientname"

But this code is wrong on so many levels. You really, REALLY (I cannot stress it enough) should be using jq for this even if that means building it from source.

TB0ne 03-25-2020 09:50 AM

Quote:

Originally Posted by Historia (Post 6104183)
I am using it for a suse 12 machine. Probably for all Suse machines.

If you mean SLES 12, that's a pay-for distro, and as such, you pay for access to the online repositories; again, why are you not wanting to download and install a package easily? And if you mean openSUSE 12, that is ANCIENT and needs to be updated as soon as possible.
Quote:

I am not good at awk and sed , that's why posted the question :(
If you never try to do anything, you will always be 'not good' at it; given that there are thousands of easily found examples for awk, grep, and sed you could find with simple search terms (like "find a line in a text file with grep" or "split text with awk"), along with the man pages, you could have made a start at trying to do this on your own. Again, we're happy to help, but don't ask us to do it for you.


All times are GMT -5. The time now is 02:02 PM.