首页 > 解决方案 > 匹配路径前缀并替换所有分隔符

问题描述

我想匹配路径前缀并使用sed.

考虑以下示例字符串和所需的结果。路径前缀是one/two,分隔符是正斜杠。

one/two/three.Something => one.two.three.Something
one/two/three/four/five.Another => one.two.three.four.five.Another

到目前为止,我已经想出了这个命令

echo one/two/three.Something | sed 's/one\/two\(.*\)\./one.two\1./g'

我如何使它产生上述结果,这可以用sed吗?

标签: sed

解决方案


请您尝试以下操作。

awk '/one\/two.*Something||\/one\/two.*Another/{gsub(/\//,".")} 1' Input_file

这将查找字符串one/two,直到something或者Another并用该行中的所有/内容.替换。

说明:为上述代码添加详细级别的说明。

awk '                                           ##Starting awk program from here.
/one\/two.*Something||\/one\/two.*Another/{     ##Checking condition if a line has one/two till Something OR one/two till Another then do following.
  gsub(/\//,".")                                ##Using global substitution to substitute all / with DOT
}
1                                               ##Mentioning 1 will print edited/non-edited lines here.
'  Input_file                                   ##Mentioning Input_file name here.

推荐阅读