简体   繁体   English

将类似于正则表达式的符号扩展为多个文字

[英]Expand regex-like notation to multiple literals

I've got a YAML file with nodes such as: 我有一个带有以下节点的YAML文件:

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). 我需要将i.can.(create|delete)替换为i.can.createi.can.delete (实例很多)。

How can I do this easily? 我如何轻松做到这一点?

这可能对您有用(GNU sed):

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

The idea is to replace all something(a|b|c|...) with lines somethinga somethingb etc. 这个想法是用somethinga somethingb等替换所有something(a|b|c|...)

awk seems to be suited here more than sed: awk似乎比sed更适合这里:

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

This will do what you wanted 这会做你想要的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM