LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-25-2020, 06:57 AM   #1
Historia
LQ Newbie
 
Registered: Mar 2020
Posts: 6

Rep: Reputation: Disabled
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

Last edited by Historia; 03-25-2020 at 07:14 AM.
 
Old 03-25-2020, 07:26 AM   #2
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,670

Rep: Reputation: Disabled
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.

Last edited by shruggy; 03-25-2020 at 07:37 AM.
 
Old 03-25-2020, 07:28 AM   #3
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,120

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
More to the point - show us what efforts you have made yourself; either jq, awk, sed, ... whatever.
 
Old 03-25-2020, 07:36 AM   #4
Historia
LQ Newbie
 
Registered: Mar 2020
Posts: 6

Original Poster
Rep: Reputation: Disabled
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
 
Old 03-25-2020, 07:37 AM   #5
Historia
LQ Newbie
 
Registered: Mar 2020
Posts: 6

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by shruggy View Post
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
 
Old 03-25-2020, 07:38 AM   #6
Historia
LQ Newbie
 
Registered: Mar 2020
Posts: 6

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by syg00 View Post
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
 
Old 03-25-2020, 07:50 AM   #7
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,623

Rep: Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964
Quote:
Originally Posted by Historia View Post
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.
 
Old 03-25-2020, 07:53 AM   #8
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,670

Rep: Reputation: Disabled
Quote:
Originally Posted by Historia View Post
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.
 
Old 03-25-2020, 08:14 AM   #9
Historia
LQ Newbie
 
Registered: Mar 2020
Posts: 6

Original Poster
Rep: Reputation: Disabled
Unhappy

Quote:
Originally Posted by TB0ne View Post
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
 
Old 03-25-2020, 08:24 AM   #10
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,804

Rep: Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306
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}'

Last edited by pan64; 03-25-2020 at 08:27 AM.
 
2 members found this post helpful.
Old 03-25-2020, 08:26 AM   #11
Historia
LQ Newbie
 
Registered: Mar 2020
Posts: 6

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by pan64 View Post
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
 
Old 03-25-2020, 08:30 AM   #12
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,670

Rep: Reputation: Disabled
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.
 
Old 03-25-2020, 09:50 AM   #13
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,623

Rep: Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964
Quote:
Originally Posted by Historia View Post
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.
 
  


Reply

Tags
json



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: JSON Lines: record-style JSON LXer Syndicated Linux News 0 01-28-2020 08:03 PM
Best Method For Query Content In Large JSON Files metallica1973 Programming 5 04-20-2016 01:10 PM
Write a script which copies content of file 1 to file 2 without using cp command. aashka Linux - Newbie 5 04-10-2012 03:55 PM
[SOLVED] How to pass file content through URL using shell script swathikumar Linux - Newbie 1 07-16-2011 01:50 PM
modify content in a file using shell script fjkum Linux - Newbie 2 08-03-2006 04:46 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration