sed - 匹配路径前缀并替换所有分隔符
问题描述
我想匹配路径前缀并使用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
吗?
解决方案
请您尝试以下操作。
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.
推荐阅读
- python-3.x - 在 SKLearn 中使用自定义转换器时的 SettingWithCopyWarning
- flutter - 在应用浏览器中隐藏功能不起作用?
- android - 进度条的自定义框架
- c - 是否有一些 C 预处理器可以提高将“this”传递给虚拟方法的要求?
- mysql - 将不同树中的两个表加入 phpmyadmin
- java - 我可以订阅 Java 中的 SseEmitter
- stormcrawler - 下载 StormCrawler 中的所有图像而不对其进行索引
- c++ - 带有 std::atomic_flag 的自旋锁 - 是否让线程进入睡眠状态?
- fix-protocol - FIX 协议和消息确认策略
- python - 在 MicroPython 中:如何识别传递了库类的哪个实例?