Expand regex-like notation to multiple literals

馋奶兔 提交于 2020-01-02 17:32:36

问题


I've got a YAML file with nodes such as:

group:
  name: test
  permissions:
  - i.can.(create|delete)

I need to replace i.can.(create|delete) with i.can.create and i.can.delete (there are many instances).

How can I do this easily?


回答1:


The idea is to replace all something(a|b|c|...) with lines somethinga somethingb etc.

awk seems to be suited here more than sed:

awk 'BEGIN { FS="[(|)]" ; OFS=""} /\(.*\)/ { for (field=2;field<NF;field++) {print $1,$field ;} ; next ; }1'

This will do what you wanted




回答2:


This might work for you (GNU sed):

sed -r 's/^(.*\bi\.can\.)\((create)\|(delete)\)/\1\2\n\1\3/' file


来源:https://stackoverflow.com/questions/22764017/expand-regex-like-notation-to-multiple-literals

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!