【问题标题】:Search for a URL in a file and replace with local file path from commandline在文件中搜索 URL 并从命令行替换为本地文件路径
【发布时间】:2022-11-04 08:58:27
【问题描述】:

我在磁盘上有一个 Python 文件,我想在函数中搜索一行并将该行中的 URL 替换为本地文件路径。

def showBuilderHelp():
    from webbrowser import open as openUrl
    openUrl('https://github.com/jobyski/public_help_v1.1.pdf')

当我尝试使用 sed

sed -i  's/https://github.com/jobyski/public_help_v1.1.pdf/file:///on/disk/path/file/public_help_v1.1.pdf/g'  thefile.py

但这会引发错误

sed: 无法打开文件https:/github.com/jobyski/public_help_v1.1.pdf/g 没有这样的文件或目录

我不是 sed 或 grep 方面的专家。

【问题讨论】:

  • 要在sed 中替换/,您需要将其转义\/ 或在" 中引用您的sed,这是我乍一看这段代码所看到的,尚未尝试过这些,但恕我直言。

标签: sed


【解决方案1】:

啊哈,这很简单:

sed -i 's/https://github.com/jobyski/file:///on/disk/path/file/g' thefile.py

更新文件。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-27
    • 2012-03-21
    • 2016-09-22
    • 2015-10-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多