0

I have a txt file which is an output from an api call to JIRA which creates a task.

In this file, I have this text (issue # can change):

"key":"JIRA-90"

I'd need to grab the JIRA-90 but can't seem to figure out how to do it with sed.

JIRA-90 can of course change as that is the issuetype in JIRA.

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
  • You are, I suspect, getting a JSON blob back from the Jira API. Kick the JSON output into jq, which is designed to parse JSON (which, being an irregular language, is something that sed is not). – DopeGhoti Feb 07 '20 at 20:00
  • Unfortunately, we are locked down and I can't install jq.. :( – Seth Efrat Feb 07 '20 at 20:21

2 Answers2

2
sed -n 's/.*"key":"\([^"]*\)".*/\1/p' < your-file

Using the usual idiom: sed -n 's/pattern/replacement/p which does a substitution and only prints the resulting pattern space if it's successful (not printing the pattern space at the end of each cycle as it would do without -n).

  • This is close. However, I'm getting all the text after the JIRA-90 value. i.e. output shows: JIRA-90","self":"https://jira.com/rest/api/2/issue/2345555"}[user@server – Seth Efrat Feb 07 '20 at 20:33
  • Then the . in the \(.*\) needs to be changed to only match expected text. Here's one way of doing that: change \(.*\) to \(\w\+-\w\+\) – JamesL Feb 07 '20 at 20:42
  • Funny enough, this produces the exact same output. – Seth Efrat Feb 07 '20 at 20:46
  • @SethEfrat, there's no such ,"self":... in the sample in your question. Please clarify what exactly you want to extract from exactly what shape of data. – Stéphane Chazelas Feb 07 '20 at 21:18
  • 1
    Out of curiosity, why indirect the file with <? sed takes a file to parse as a parameter. – DopeGhoti Feb 07 '20 at 21:56
  • 2
    @DopeGhoti https://unix.stackexchange.com/a/458268/117549 – Jeff Schaller Feb 08 '20 at 00:02
  • Hi @StéphaneChazelas, the file that curl outputs to has quite a bit of header info and at the bottom is this line: {"id":"1234567","key":"JIRA-90","self":"https://jira.com/rest/api/2/issue/1234567 So, I'm just trying to get the text after "key" and before ,"self" Again, I appreciate your help! – Seth Efrat Feb 10 '20 at 12:58
  • Thank you Stéphane for contributing your time and effort on this. While it didn't directly solve my issue, it did help me get a better understanding of sed – Seth Efrat Feb 12 '20 at 12:52
0

Assuming your input file contains more than 1 name-value pair, consider this:

$ awk -F'":"' '{gsub(/^[[:space:]]*"|"[[:space:]]*$/,""); f[$1]=$2} END{print f["key"]}' file
JIRA-90

That approach lets you populate an array (f[] above) with the name to value mappings and then you can just print the values by their names whenever you like.

$ cat file
        "foo":"127"
        "key":"JIRA-90"
        "bar":"hello world"

$ awk -F'":"' '{gsub(/^[[:space:]]*"|"[[:space:]]*$/,""); f[$1]=$2} END{print f["bar"], f["key"]}' file
hello world JIRA-90

$ awk -F'":"' '{gsub(/^[[:space:]]*"|"[[:space:]]*$/,""); f[$1]=$2} END{print f["foo"] * 3}' file
381

$ awk -F'":"' '{gsub(/^[[:space:]]*"|"[[:space:]]*$/,""); f[$1]=$2} END{for (i in f) print i "=" f[i]}' file
key=JIRA-90
foo=127
bar=hello world
Ed Morton
  • 31,617
  • Thank you! I tried using your solution but, I get an empty line returned so it doesn't seem to pick up what is after "key" – Seth Efrat Feb 10 '20 at 12:55
  • Then your posted sample input doesn't adequately reflect your real input. When posting sample input/output it's important to show the text you want to find in context among the text you want to find it in. – Ed Morton Feb 10 '20 at 14:44
  • 1
    Thank you Ed for providing these examples. I appreciate the time you put into this and I'll be sure to pay it forward in the future :) – Seth Efrat Feb 12 '20 at 12:53