-1

How can I use sed to extract f607b9ab-1452-4758-b047-6b620591211b by using cmrepo-222-02 as my reference?

["+--------------------------------------+---------------+----------+------+-------------+", "| ID                                   | Name          | Status   | Size | Attached to |", "+--------------------------------------+---------------+----------+------+-------------+", "| f607b9ab-1452-4758-b047-6b620591211b | cmrepo-222-02 | creating |    1 |             |", "| 5e9b27fd-46b4-4950-9dc1-a838be21aff7 | cmrepo-222-01 | creating |    1 |             |", "+--------------------------------------+---------------+----------+------+-------------+"]
New contributor
Rajabu Kitindi is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
1
  • 1
    SInce you didn't use a code block, the formatting of your input document is hard to decipher, which makes the question even harder to answer. But you need to be clearer about your requirements. Is there just one string like this you want to extract, or are there many? What is known in advance about the structure of the input file? Is it always the same number of lines, etc.? Describe your problem more explicitly, or it is very hard to answer.
    – frabjous
    May 12 at 14:38

2 Answers 2

1

It looks like a json array of strings representing the lines of the output (intended for human consumption, not for post-processing) of some SQL query. So you could use jq to decode the json and awk to attempt to extract data from that formatted table.

<that-file jq -r '.[]' - |
  awk -F '[[:space:]]*[|][[:space:]]*' '$3 == "cmrepo-222-02" {print $2}'

But best would be to do the right SQL query in the first place.

0

Using sed

$ ref=cmrepo-222-02
$ sed "s/.* \([[:alnum:]-]\+\).*$ref.*/\1/" input_file
f607b9ab-1452-4758-b047-6b620591211b
2

Your Answer

Rajabu Kitindi is a new contributor. Be nice, and check out our Code of Conduct.

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.