【问题标题】:How to add some string before a particular type of words?如何在特定类型的单词之前添加一些字符串?
【发布时间】:2020-05-05 10:09:41
【问题描述】:

我有一个文件,其内容是:-

abc"/wiki/A"def
ghi"/wiki/B"jkl
mno"/wiki/C"pqr

我想在给定句子的所有 /wiki/ 之前添加“https://en.wikipedia.org”。 文件中可以有更多这些类型的字符串。

要求的结果是:-

abc"https://en.wikipedia.org/wiki/A"def
ghi"https://en.wikipedia.org/wiki/B"jkl
mno"https://en.wikipedia.org/wiki/C"pqr

我尝试了一些解决方案:-

How to use sed command to add a string before a pattern string?

Insert text before a certain line using Bash

根据我的猜测,它与“wiki”之前的“/”字符有关。忽略“/”它工作得很好。

我提供的解决方案是:-

sed 's//wiki//bar/wiki//g' abcdef

但显示的错误是

sed: -e expression #1, char 13: unknown option to `s'

【问题讨论】:

标签: shell ubuntu sed


【解决方案1】:

在/(斜杠)之前添加\(反斜杠)以识别不同的含义/在sed中。

如果您在终端上运行,则必须执行 -e 选项。这是手动 sed。

-e CMD 添加CMD到sed要执行的命令

sed -e 's/wiki/https\:\/\/en.wikipedia.org\/wiki/g'

【讨论】:

  • 感谢您的回复;但它不起作用,我得到的是“/en.wikipedia.org/wiki/A”而不是“en.wikipedia.org/wiki/A”;这样 http....... . 之前的额外 / (斜杠)。
  • 您只需要在“/”之前添加“\”即可。
【解决方案2】:

请试试这个:-

sed 's/\/wiki/https:\/\/en.wikipedia.org\/wiki/g' filename

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-08-02
    • 2022-09-24
    • 2021-10-28
    • 1970-01-01
    • 2023-04-10
    • 1970-01-01
    • 2018-09-30
    相关资源
    最近更新 更多