【发布时间】:2021-02-28 21:09:13
【问题描述】:
我遇到了以下问题;
作为输入,我得到几行文件路径如下:
root/child/abc/somefile.txt
root/child/def/123/somefile.txt
root/child/ghijklm/somefile.txt
root/child 始终在路径中,之后的一切都可能不同。
我想删除孙文件夹之后的所有内容。所以输出将是:
root/child/abc/
root/child/def/
root/child/ghijklm/
我尝试了以下方法:
sed 's/\/child\/.*/\/child\/.*/'
当然,这只会给出以下输出:
root/child/.*
root/child/.*
root/child/.*
任何帮助将不胜感激!
【问题讨论】:
-
使用 GNU awk:
awk '{NF=3; NF++}1' FS=/ OFS=/ file