1

I have output.txt file, where my script is storing some outputs, I just need to get the output of ID which is in the 1st line of the output.txt in myscript.sh file, can someone suggest a way to do that

{"id":"**dad04f6d-4e06-4420-b0bc-cb2dcfee2dcf**","name":"new","url":"https://dev.azure.com/vishalmishra2424/82c93136-470c-4be0-b6da-a8234f49a695/_apis/git/repositories/dad04f6d-4e06-4420-b0bc-cb2dcfee2dcf","project":{"id":"82c93136-470c-4be0-b6da-a8234f49a695","name":"vishalmishra","url":"https://dev.azure.com/vishalmishra2424/_apis/projects/82c93136-470c-4be0-b6da-a8234f49a695","state":"wellFormed","revision":12,"visibility":"public","lastUpdateTime":"2021-04-22T14:24:47.2Z"},"size":0,"remoteUrl":"https://[email protected]/vishalmishra2424/vishalmishra/_git/new","sshUrl":"[email protected]:v3/vishalmishra2424/vishalmishra/new","webUrl":"https://dev.azure.com/vishalmishra2424/vishalmishra/_git/new","isDisabled":false}
3
  • 1
    What have you searched for, and what did you find? What have you tried, and how did it fail? Commented May 11, 2021 at 11:42
  • I searched few docs and tried the below one, but its not giving me what i am expecting while read line; do [[ $line =~ id ]] && declare "$line" && break done < Repooutput.txt Commented May 11, 2021 at 11:56
  • 2
    I suggest: jq -r '.id' output.txt Commented May 11, 2021 at 11:57

1 Answer 1

2

The snippet you posted looks like JSON and a utility named file which can guess different types of file says that too:

$ file output.txt
output.txt: JSON data

You should use JSON-aware tools to extract value of id, for example jq:

$ jq -r '.id' output.txt
**dad04f6d-4e06-4420-b0bc-cb2dcfee2dcf**

or jshon:

$ jshon -e id < output.txt
"**dad04f6d-4e06-4420-b0bc-cb2dcfee2dcf**"
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you so much really appreciate your help
When I running this directly in shell, then its working fine. But when I am including it in script getting access denied. Any idea what I am doing wrong here. $ jq -r '.id' Repooutput.txt dad04f6d-4e06-4420-b0bc-cb2dcfee2dcf
$ sh test.sh test.sh: line 3: /c/ProgramData/chocolatey/bin/jq: Permission denied
Hmm, I have no idea. It might be Windows issue but I don't know how to use Windows, sorry.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.