简体   繁体   English

sed 和正则表达式规则替换文本

[英]sed and regex rule to substitute text

I have the following text:我有以下文字:

Before doing anything, curses must be initialized.在做任何事情之前,curses 必须被初始化。 This is done by calling the:func:`~curses.initscr` function, which will determine the terminal type, send any required setup codes to the terminal, and create various internal data structures.这是通过调用:func:`~curses.initscr` function 来完成的,它将确定终端类型,将任何所需的设置代码发送到终端,并创建各种内部数据结构。 If successful, :func:`initscr` returns a window object representing the entire screen;如果成功,:func:`initscr` 返回一个代表整个屏幕的 window object; this is usually called ``stdscr`` after the name of the corresponding C variable.这通常在相应的 C 变量的名称之后称为“stdscr”。

I want replace:我要更换:

:func:`~curses. :func:`~诅咒。 initscr `初始化`

with:和:

`initscr https://docs.python.org/3/library/curses.html#curses.initscr `_ `initscr https://docs.python.org/3/library/curses.html#curses.initscr `_

and I tried the following sed rule:我尝试了以下 sed 规则:

's@ :func:`~curses\.\(.*\)`@ `\1 <https://docs.python.org/3/library/curses.html#curses.\1>`_ @g'

but the following is the result:但结果如下:

Before doing anything, curses must be initialized.在做任何事情之前,curses 必须被初始化。 This is done by calling the `initscr` function, which will determine the terminal type, send any required setup codes to the terminal, and create various internal data structures.这是通过调用`initscr` function 来完成的,它将确定终端类型,将任何所需的设置代码发送到终端,并创建各种内部数据结构。 If successful, :func:`initscr` returns a window object representing the entire screen;如果成功,:func:`initscr` 返回一个代表整个屏幕的 window object; this is usually called ``stdscr` <https://docs.python.org/3/library/curses.html#curses.initscr` function, which will determine the terminal type, send any required setup codes to the terminal, and create various internal data structures.这通常称为``stdscr` <https://docs.python.org/3/library/curses.html#curses.initscr` function,它将确定终端类型,将任何所需的设置代码发送到终端,以及创建各种内部数据结构。 If successful, :func:`initscr` returns a window object representing the entire screen;如果成功,:func:`initscr` 返回一个代表整个屏幕的 window object; this is usually called ``stdscr`>`_ after the name of the corresponding C variable.这通常在相应的 C 变量的名称之后称为 ``stdscr`>`_。 :: ::

Use利用

sed -E 's@ :func:`~curses\.([^`]+)`@ `\1 <https://docs.python.org/3/library/curses.html#curses.\1>`_ @g' file

See demo演示

EXPLANATION解释

NODE                     EXPLANATION
--------------------------------------------------------------------------------
   :func:`~curses          ' :func:`~curses'
--------------------------------------------------------------------------------
  \.                       '.'
--------------------------------------------------------------------------------
  (                        group and capture to \1:
--------------------------------------------------------------------------------
    [^`]+                    any character except: '`' (1 or more
                             times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
  )                        end of \1
--------------------------------------------------------------------------------
  `                        '`'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM