-3

I need to get all node names from yaml file which has a particular tag in properties. This is the structure of YAML file:

node1:  
    description: "App Server"
     ******************** /many lines/ ********
    tags:  "web_server,file_server"
node2:  
    description: "Server"
    ******************** /many lines/ ******** 
    tags: "nginx,nginx_a,nginx_b"
node3:
    description: "Server"
    ******************** /many lines/ ******** 
    tags: "application1,application2"
    and so on 

How to get node names which have tag "nginx" inside? We can read the file from the end , if we have got tag name than get first pattern ^[a-z], and start for searching tag again.

ckopn
  • 1
  • 1
  • Please explain what you mean by node name. In YAML scalar like the double quoted scalar `"ing_deals,ing_deals_b,ing_side_b,ing_all"` can have a tag (but none of your scalars have tags according the the YAML specification), but there is no concept of "name" of a node in the YAML specification. – Anthon Mar 27 '19 at 11:43
  • And there is also no need in YAML that the key and value of mapping be on the same line, nor all parts of a scalar. Although *your* particular input *currently* might have such restrictions, it is not very wise to rely on that. Can you confirm that when you write "with bash" in the title, you mean bash's built-in functionality **only** and not using any program started from bash (such as perl, python, sed, grep, yq), – Anthon Mar 27 '19 at 11:48
  • Node names are values in the root of yaml structure: sfsdf3434-dc, c65655cd, c35gfg7wkz, c6dg06trx. Sed, awk, grep are allowed, not python. – ckopn Mar 27 '19 at 12:16
  • 1
    To properly parse YAML, you need a YAML implementation. Implementations are available as libraries for various programming languages. By ruling out the usage of such programming languages (like Python), you're basically saying that you want to write a YAML implementation in bash (or awk), which is possible, but well beyond the scope of a SO question. Mind that you cannot parse YAML with regular expressions ([just like with XML](https://stackoverflow.com/a/1732454/347964)). – flyx Mar 27 '19 at 12:40
  • I don't need yaml implementation, I can do it with python without yaml module. Read file by line from the end; if the line has "word" inside, get the next line starting with letters(other lines have 4 indentations) and having a colon in the end. – ckopn Mar 27 '19 at 12:47
  • 1
    Then your question is not about YAML but about parsing a specifically structured text which also happens to be valid YAML. Such code will fail for general YAML documents if they are not presented exactly like your input is. If you already know an algorithm that does work on your input, you should describe it in your question so that people know what you actually want to do. – flyx Mar 27 '19 at 12:58
  • @ckopn The root of your YAML consists of a mapping node. That mapping node, as it is not empty, has keys (all scalar nodes in your example) and values (all mapping nodes in your example). Please use appropriate terminology: writing "node names are values" for something that are *keys* and are not names, makes no sense at all. – Anthon Mar 27 '19 at 14:19
  • My friend already parsed this yaml file with regular expressions but is it not possible with sed? – ckopn Mar 27 '19 at 17:45
  • Use [**yq**](https://github.com/kislyuk/yq). – John Kugelman Mar 29 '19 at 12:03

1 Answers1

0
tac file.yaml | awk '/tag/&&/tag_name/{f=1;next} /^[a-z]/{ if (f) {print;f=0 } }' | sed 's/:\r//g')

I did this with tac, awk, and sed. tac - revert the file. awk makes all logic. sed removes colon, and /r in the end.

ckopn
  • 1
  • 1