【问题标题】:How to remove everything from path after a word using sed in bash/ shell script?如何在 bash/shell 脚本中使用 sed 从一个词后的路径中删除所有内容?
【发布时间】:2023-01-12 06:04:30
【问题描述】:

单词重复且只想从特定单词位置删除的地方

Lets say my path is - /opt/xyz/config/config.xml 
Solution I want after using sed is: /opt/xyz/config/

如何获得?

我讨厌使用 {sed 's/config.*//'} >> 这实际上删除了两个配置字 就像它看起来

/opt/xyz/

我尝试过以多种方式使用它

>  sed 's/config.*//'

【问题讨论】:

  • echo /opt/xyz/config/config.xml | sed 's/config\.xml//'

标签: linux bash shell sed


【解决方案1】:

也许用这样的东西?

sed 's/[^/]*$//'

但是如果文件路径在 shell 变量中,那么你也可以使用:

mydir=${myfilepath%/*}/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-03-11
    • 2016-04-13
    • 2017-09-05
    • 1970-01-01
    • 2021-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多