How to replace patterns matching `{ + (anything) + }` with `{ + (anything) + :: + (anything) + }` in BASH?
我有一个带有一些文本的文件,其中一些重要的项目标有大括号和右括号,例如:
1 2 3 4 | Once upon a {time}, there lived a rabbit. The {rabbit lived} in a small house. One day, the {rabbit visited }the {mountains}. In the mountains, he {found} a tree with 10{,000} branches. |
我需要用
1 2 3 4 | Once upon a {time::time}, there lived a rabbit. The {rabbit lived::rabbit lived} in a small house. One day, the {rabbit visited ::rabbit visited }the {mountains::mountains}. In the mountains, he {found::found} a tree with 10{,000::,000} branches. |
-
每个开口
{ 在同一行上都有一个匹配的} 。 - 大括号永不分开。
- 大括号永远不会嵌套。
-
在
{ 和} 之间可能出现任何类型的符号。
我尝试了
1 | sed 's/{(.*)}/{&::&}/g' file.txt |
如何替换括号中的所有项目,例如
这是解决方法
1 2 3 4 5 6 | sed 's/{\\([^}]*\\)}/{\\1::\\1}/g' file Once upon a {time::time}, there lived a rabbit. The {rabbit lived::rabbit lived} in a small house. One day, the {rabbit visited ::rabbit visited }the {mountains::mountains}. In the mountains, he {found::found} a tree with 10{,000::,000} branches. |
解释
-
[^}]* 匹配非} 字符 -
\\(...\\) 将捕获在括号内指定的字符,而\\\\ 1将用于引用第一个匹配项,这是regex的一部分。
如果可以使用
1 2 3 4 5 | $ perl -ple 's/{(.*?)}/{$1::$1}/g' file Once upon a {time::time}, there lived a rabbit. The {rabbit lived::rabbit lived} in a small house. One day, the {rabbit visited ::rabbit visited }the {mountains::mountains}. In the mountains, he {found::found} a tree with 10{,000::,000} branches. |
它匹配花括号
1 2 3 4 5 6 | $ awk 'BEGIN{ORS=""} NR%2==0{$0="{"$0"::"$0"}"} 1' RS='[{}]' file.txt Once upon a {time::time}, there lived a rabbit. The {rabbit lived::rabbit lived} in a small house. One day, the {rabbit visited ::rabbit visited }the {mountains::mountains}. In the mountains, he {found::found} a tree with 10{,000::,000} branches. |
您应该使用
1 | sed 's/\\([^{]*{\\)\\([^}]*\\)\\(}.*\\)/\\1\\2::\\2\\3/' |
未测试