fork(4) download
  1. #!/bin/bash
  2. string="name: MAIN_ROLE
  3. description: ROLE DESCRIPTION
  4. readOnly:
  5. roleReferences:
  6. - roleTemplateAppId: app1
  7. roleTemplateName: template1
  8. name: Name1
  9. - roleTemplateAppId: app2
  10. roleTemplateName: template2
  11. name: Name2
  12. "
  13. awk 'NR==1{ # When on Line 1
  14. a=$2;cnt=0 # Set a and cnt vars
  15. }
  16. /^-/{ # When line starts with -
  17. rta[cnt]=$3; getline; # Add role template app ID to rta array
  18. rtn[cnt]=$2; getline; # Add role template name to rtn array
  19. n[cnt]=$2;cnt++ # Add name to n array
  20. }
  21. END{ # When the file processing is over
  22. for(i=0;i<cnt;i++) { # Iterate over the found values and...
  23. print a","n[i]","rtn[i]","rta[i] # print them
  24. }
  25. }' <<< "$string"
  26.  
  27. # => MAIN_ROLE,Name1,template1,app1
  28. # MAIN_ROLE,Name2,template2,app2
Success #stdin #stdout 0.01s 5472KB
stdin
Standard input is empty
stdout
MAIN_ROLE,Name1,template1,app1
MAIN_ROLE,Name2,template2,app2