sed 和正则表达式中逗号“,”的所有含义是什么?
What are all of the meanings for commas "," in sed and regex?
此脚本用于 Arduino install.sh 文件 IDE:
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 月)的压缩包。
逗号有什么作用?有没有人对 sed 和 regex 在所有情况下的逗号功能有很好的参考?
以下是我如何分解命令和我一直在搜索的所有地方。网上有很多关于如何处理包含逗号的信息或向信息添加逗号的参考资料。逗号的功能不太容易找到。搜索“,”或 "comma" 并没有让我走得太远。这个 Stack Overflow 问题旨在添加某人可以找到的搜索结果。我的兴趣是学术。
***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 中创建一个 "repeat range"
- 例如匹配 2-5 个
a
的:/a\{2,5\}/
,5 个或更多:/a\{5,\}/
顺便说一句,你的故障的后半部分已经过去了。这是更正:
- 在shell中,双引号字符串进行参数扩展,所以
${SCRIPT_PATH}
表示一个变量
<>
- 这些是文字。你在想 \<\>
g
- "global" - s
:
的标志
Apply the replacement to all matches to the regexp, not just the first.
此脚本用于 Arduino install.sh 文件 IDE:
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 月)的压缩包。
逗号有什么作用?有没有人对 sed 和 regex 在所有情况下的逗号功能有很好的参考?
以下是我如何分解命令和我一直在搜索的所有地方。网上有很多关于如何处理包含逗号的信息或向信息添加逗号的参考资料。逗号的功能不太容易找到。搜索“,”或 "comma" 并没有让我走得太远。这个 Stack Overflow 问题旨在添加某人可以找到的搜索结果。我的兴趣是学术。
***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 givens
command.
Arduino 脚本可能使用备用分隔符来避免转义 $SCRIPT_PATH
中的斜杠,这也不太可能包含逗号。比较:
s/<BINARY_LOCATION>/\/some\/path\/arduino/
s,<BINARY_LOCATION>,/some/path/arduino,
也可以用逗号:
- 创建 address range
- 例如删除第 2-5 行:
2,5d
- 例如删除第 2-5 行:
- 在大括号之间的 regex 中创建一个 "repeat range"
- 例如匹配 2-5 个
a
的:/a\{2,5\}/
,5 个或更多:/a\{5,\}/
- 例如匹配 2-5 个
顺便说一句,你的故障的后半部分已经过去了。这是更正:
- 在shell中,双引号字符串进行参数扩展,所以
${SCRIPT_PATH}
表示一个变量 <>
- 这些是文字。你在想\<\>
的标志g
- "global" -s
:Apply the replacement to all matches to the regexp, not just the first.