regex - sed 和 regex 中逗号 ","的所有含义是什么?

标签 regex sed comma

此脚本用于 Arduino IDE 的 install.sh 文件:

  sed -e "s,<BINARY_LOCATION>,\"${SCRIPT_PATH}/arduino\",g" \
      -e "s,<ICON_NAME>,${RESOURCE_NAME},g" "${SCRIPT_PATH}/lib/desktop.template" > "${TMP_DIR}/${RESOURCE_NAME}.desktop"

该文件来自 Linux 64 位发行版 1.8.12(当前,2020 年 4 月)的 tarball。

逗号有什么作用?有没有人对 sed 和 regex 在所有情况下的逗号功能有很好的引用?

以下是我如何分解命令和我一直在搜索的所有地方。网络上有很多关于如何处理包含逗号的信息或向信息添加逗号的引用资料。逗号的功能不太容易找到。搜索“,”或“逗号”并没有让我走得很远。此堆栈溢出问题旨在添加某人可以找到的搜索结果。我的兴趣是学术。

***edit note: some of this is wrong. see answer from wjandrea for 
              corrections
sed - stream editor for filtering and transforming text (1)
    -e script, --expression=script
       add the following script to the commands to be executed (1) 
    s substitution
       Its basic concept is simple: the s command attempts to match 
       the pattern space against the supplied regular expression 
       regexp; if the match is successful, then that portion of the 
       pattern space which was matched is replaced with replacement(2,3)
    ,  ???
    <> RegExpression for \< match beginning of word \> end of word (4)
    $  Match the last line. 
       To ALLOW the Unix shell to interpret the dollar sign, put the 
       script in double quotes. sed "s/_terminal-type_/$TERM/g" (5)
    {} A group of commands may be enclosed between { and } characters.
       This is particularly useful when you want a group of commands 
       to be triggered by a single address (or address-range) match.(6)
    g  Copy/append hold space to pattern space. (1) Replace the 
       contents of the pattern space with the contents of the hold 
       space. (7) - find "Special Applications:"
  • (1) https://linux.die.net/man/1/sed
  • (2) http://sed.sourceforge.net/sedfaq4.html#s4.30.1
  • (3) https://www.gnu.org/software/sed/manual/html_node/The-_0022s_0022-Command.html#The-_0022s_0022-Command
  • (4) https://www.gnu.org/software/sed/manual/html_node/regexp-extensions.html#regexp-extensions
  • (5) https://www.gnu.org/software/sed/manual/html_node/BRE-syntax.html#BRE-syntax
  • (6) https://www.gnu.org/software/sed/manual/sed.html#Common-Commands
  • (7) http://sed.sourceforge.net/sed1line.txt
  • sed 中的正则表达式:https://www.gnu.org/software/sed/manual/html_node/sed-regular-expressions.html#sed-regular-expressions
  • 最佳答案

    在sed中, s 后面的字符用作分隔符。斜线 /是标准。

    The / characters may be uniformly replaced by any other single character within any given s command.



    Arduino 脚本可能使用备用分隔符来避免在 $SCRIPT_PATH 中转义斜杠, 也不太可能包含逗号。相比:
    s/<BINARY_LOCATION>/\/some\/path\/arduino/
    s,<BINARY_LOCATION>,/some/path/arduino,
    

    也可以使用逗号:
  • 创建一个 address range
  • 例如删除第 2-5 行:2,5d
  • regex在大括号之间创建“重复范围”
  • 例如匹配 2-5 a的:/a\{2,5\}/ , 5 个或更多:/a\{5,\}/


  • 顺便说一句,您崩溃的后半部分还很遥远。这是一个更正:
  • 在 shell 中,双引号字符串受参数扩展的影响,所以 ${SCRIPT_PATH}代表一个变量
  • <> - 这些都是字面意思。你在想 \<\>
  • g - “全局” - 标志 s :

    Apply the replacement to all matches to the regexp, not just the first.

  • 关于regex - sed 和 regex 中逗号 ","的所有含义是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61512558/

    相关文章:

    linux - 使用 grep 提取括号内的数字

    c# - 将十进制转换为字符串,十进制后加上两个零,并且使用逗号? C#

    python - 使用逗号 Python 解析文件

    list - 使用两个反引号和逗号,Common Lisp

    java - 从命令窗口输出中获取特定子字符串(来自 ping 的时间)

    awk - 将 awk 应用于除第一行以外的所有内容

    shell - sed:重命名文件中的选择性字符串

    python - 如何使用正则表达式提取 img 标签中的 src?

    regex - 正则表达式 rel 标签

    python - 如何在Python中使用正则表达式获取两个特定字符之间的第一个整数?