regex - 匹配特定模式后的第一个'/'
问题描述
尝试在 bash 脚本中使用 sed 正则表达式在 macos 中执行此操作。我有一个包含外部驱动器目录列表的文件。
/Volumes/WD/A/Some Learning/Learning Is Great/
/Volumes/WD/A/Some Deeper/Learning Is Great/Another Learning Opportunity/
/Volumes/WD/B/More Things Here/Great Learning/
/Volumes/WD/B/More Things/
我想搜索并返回匹配各种模式的最顶层目录。如果您搜索“学习”,则输出应为:
/Volumes/WD/A/Some Learning/
/Volumes/WD/A/Some Deeper/Learning Is Great/
/Volumes/WD/B/More Things Here/Great Learning/
解决方案
如果您觉得awk
可以,请尝试以下操作。
awk 'match($0,/Learning[^\/]*\//){print substr($0,1,RSTART+RLENGTH-1)}' Input_file
输出如下。
/Volumes/WD/A/Some Learning/
/Volumes/WD/A/Some Deeper/Learning Is Great/
/Volumes/WD/B/More Things Here/Great Learning/
推荐阅读
- android-layout - 如何每行并排列出布局中的项目 - android
- c++ - 我应该使用什么样的数据结构来实现 UPGMA?
- c# - 您的 SQL 语法错误,.net 的 mysql 连接器
- hibernate - JpaRepository 进行意外更新
- session - 当我们已经有 cookie 时,为什么还需要 session ?
- c# - 为什么 C# 编译器会为 NULL 和数字比较产生结果?
- angular - GZip 在 Webpack 2.5.1 中不起作用
- android - MPAndroidChart BarChart 在重绘时不考虑条形宽度
- javascript - 使用 jquery 关闭当前模态并打开新模态
- java - 使用spring数据mongodb进行组聚合