Reputation: 741
I have a JSON like below coming from curl command and it is present in a output.txt file. I want to retreive the JIRA status, here it is "In Progress"
{
"self": "https://jira.com/jira/rest/api/2/issue/1",
"fields": {
"status": {
"self": "https://jira.com/jira/rest/api/2/status/10170",
"description": "",
"name": "In Progress",
"id": "10170"
}
}
}
I have a restriction to use only sed . I tried like below it does not work . I am not sure how to navigate to the name value. can you please suggest to print JIRA status
sed -n 's|.*"fields":{"status":{"name":"\([^"]*\)".*|\1|p' output.txt
Upvotes: 2
Views: 2321
Reputation: 626851
You can use
sed -n 's/^[[:space:]]*"name": "\(.*\)",/\1/p' output.txt
# With GNU sed:
sed -n 's/^\s*"name":\s*"\(.*\)",/\1/p' output.txt
See the online demo
Details:
n
- suppresses default line output^\s*"name":\s*"\(.*\)",
- matches
^
- start of string\s*
- zero or more whitespaces"name":
- a literal string\s*
- zero or more whitespaces"
- a "
char\(.*\)
- a POSIX BRE capturing group matching any text up to",
- the last occurrence of ",
(they are at the end of the targeted line anyway).\1
- replaces the whole match with Group 1 valuep
- only prints the replacement result.With a GNU sed
, you can also use the -z
option to read the file as a single string and then use a more specific pattern:
sed -z 's/.*"fields":\s*{\s*"status": {.*\s*name": "\([^"]*\)".*/\1/' output.txt
See this online demo.
It does something very close to this demo.
Upvotes: 2