LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 08-08-2019, 01:03 AM   #1
Anna$9
LQ Newbie
 
Registered: Nov 2017
Posts: 24

Rep: Reputation: Disabled
How to extract the content between the quotes


Hi,
I am trying to print the OCI id: alone

oci=$(curl -L http://169.254.169.254/opc/v1/instance/ | grep '"id"
export VMid=$oci
O/P is
Code:
"id" : "ocid1.instance.oc1.iad.aggr3vsuacisb7an5mqii7cbgtivxdlpszs42esa",
But I want only this

Code:
ocid1.instance.oc1.iad.aggr3vsuacisb7an5mqii7cbgtivxdlpszs42esa
I can use cut, and it works
Code:
oci=$(curl -L http://169.254.169.254/opc/v1/instance/ | grep '"id" :' |cut -c11-87)
But I will never be able to determine the number of character will be the same always. If the next id has to be c11-93, my code is not reusable.
Kindly help me in fetching,
ocid1.instance.oc1.iad.aggr3vsuacisb7an5mqii7cbgtivxdlpszs42esa
No matter how many characters I have so that my code is reusable.
Thanks
Anna

Last edited by Anna$9; 08-08-2019 at 01:09 AM.
 
Old 08-08-2019, 01:46 AM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 18,379

Rep: Reputation: 3042Reputation: 3042Reputation: 3042Reputation: 3042Reputation: 3042Reputation: 3042Reputation: 3042Reputation: 3042Reputation: 3042Reputation: 3042Reputation: 3042
Typically that would require regex to account for the unknown number of characters. awk using the double quote as a separator will be less esoteric if you don't know regex. Something like
Code:
awk -F'"' '/id/ {print $4}'
 
1 members found this post helpful.
Old 08-08-2019, 01:48 AM   #3
berndbausch
Senior Member
 
Registered: Nov 2013
Location: Tokyo
Distribution: A few
Posts: 3,894

Rep: Reputation: 1092Reputation: 1092Reputation: 1092Reputation: 1092Reputation: 1092Reputation: 1092Reputation: 1092Reputation: 1092
You can use cut with the double quote as delimiter:
Code:
oci=$(curl ... | grep '"id"' | cut -d\" -f4)
There are other solutions using sed or awk.

EDIT: This is Oracle cloud, right? In this case, you can remove the grep by adding id to the URL. Another option is the jq program:
Code:
curl .... | jq -r .id

Last edited by berndbausch; 08-08-2019 at 02:03 AM.
 
1 members found this post helpful.
Old 08-08-2019, 08:30 AM   #4
Contrapak
Member
 
Registered: May 2019
Location: /home/
Distribution: Arch Linux
Posts: 134

Rep: Reputation: 53
Quote:
Originally Posted by berndbausch View Post
EDIT: This is Oracle cloud, right? In this case, you can remove the grep by adding id to the URL. Another option is the jq program:
Code:
curl .... | jq -r .id
Is the response in JSON though? OP doesn't say.
 
Old 08-08-2019, 10:44 AM   #5
berndbausch
Senior Member
 
Registered: Nov 2013
Location: Tokyo
Distribution: A few
Posts: 3,894

Rep: Reputation: 1092Reputation: 1092Reputation: 1092Reputation: 1092Reputation: 1092Reputation: 1092Reputation: 1092Reputation: 1092
Quote:
Originally Posted by Contrapak View Post
Is the response in JSON though? OP doesn't say.
Oracle cloud documentation says so.
 
Old 08-09-2019, 12:13 AM   #6
Sefyir
Member
 
Registered: Mar 2015
Distribution: Linux Mint
Posts: 607

Rep: Reputation: 301Reputation: 301Reputation: 301Reputation: 301
Quote:
Originally Posted by berndbausch View Post
The example in there is malformed json, but assuming curl returns valid json, this is a hacky way of getting the result. It just loads the json string and returns the value of "id"

Code:
curl -L http://169.254.169.254/opc/v1/instance/ | python -c 'import sys, json; print(json.load(sys.stdin, strict=False)["id"])'


# curl -L http://169.254.169.254/opc/v1/instance/ \
#   | python -c \                                                                 
# '                                                                               
# import sys, json                                                                
# print(                                                                          
#       json.load(sys.stdin, strict=False)["id"])                                 
# '
(depending on valid json) returns

Code:
ocid1.instance.oc1.phx.abyhqljrkfpg67546xizk4welg3n4yft4hkud6hrdj5tietdnt7s4inffjoq

Last edited by Sefyir; 08-09-2019 at 12:19 AM.
 
  


Reply

Tags
bash, shell


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
[SOLVED] Difference between echo of file within double quotes and without double quotes ankitpandey Programming 2 01-11-2013 10:02 AM
Yet Another Bash Quotes Within Quotes Issue tboyer Linux - Software 17 11-03-2012 12:17 PM
Double Quotes Inside Double Quotes youarefunny Programming 6 06-09-2010 11:21 PM
Problems with quotes and double quotes Andruha Slackware 6 01-02-2010 05:44 PM
Using single quotes vs double quotes in PHP strings vharishankar Programming 6 07-11-2005 12:41 PM

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

All times are GMT -5. The time now is 03:25 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration