简体   繁体   English

使用包含空格 (\\s) 和 sed 的正则表达式的变量

[英]Using Variables with Regex that contain a space (\s) and sed

Im trying to create a sort script using literal string variables and Regex and a sort using sed in bash.我正在尝试使用文字字符串变量和正则表达式创建排序脚本,并在 bash 中使用 sed 进行排序。 I cannot seem to find the liternal strings with spaces when using variables, although can find them when using the regex directly.使用变量时,我似乎无法找到带空格的文字字符串,尽管在直接使用正则表达式时可以找到它们。 So :所以 :

#!/bin/bash
group1="IRISHFHD"
group2="REGIONAL FHD"

sed -i '/group-title="'${group1}/',+1d' JWLINE.m3u
sed -i '/group-title="'${group2}/',+1d' JWLINE.m3u

Ive tried adding \\s into the group variable but it doesnt work.我试过将 \\s 添加到组变量中,但它不起作用。

John约翰

The problem has nothing to do with regex, it's all down to how the shell treats variables' values.问题与正则表达式无关,这完全取决于 shell 如何处理变量的值。 When a variable is expanded without double-quotes around it (ie ${group2} ), the shell will split it into "words" based on whitespace.当一个变量在没有双引号的情况下展开时(即${group2} ),shell 将根据空格将其拆分为“单词”。 It'll also try to expand any words that contain shell wildcards into lists of matching files, and several regex metacharacters look like shell wildcards, which can cause serious chaos.它还会尝试将任何包含 shell 通配符的单词扩展到匹配文件的列表中,并且几个正则表达式元字符看起来像 shell 通配符,这可能会导致严重的混乱。

In this example:在这个例子中:

sed -i '/group-title="'${group2}/',+1d' JWLINE.m3u

It's a little more complicated, because the variable reference is in between two single-quoted sections.它有点复杂,因为变量引用位于两个单引号部分之间。 In this case, the part before the variable reference gets attached to the first "word" in the variable, and the part after gets attached to the last word.在这种情况下,变量引用之前的部分附加到变量中的第一个“单词”,之后的部分附加到最后一个单词。 Essentially, it expands into the equivalent of this:本质上,它扩展为等价物:

sed -i '/group-title="REGIONAL' 'FHD/,+1d' JWLINE.m3u
                               ^ That's a space between arguments

Anyway, since it gets split on the whitespace, sed gets two partial arguments instead of one whole one, and it doesn't work at all.无论如何,由于它在空白处被分割, sed得到两个部分参数而不是一个完整参数,它根本不起作用。

Solution: as in almost all situations, you should have double-quotes around the variable reference to prevent weird effects like this.解决方案:几乎在所有情况下,您都应该在变量引用周围加上双引号,以防止出现这种奇怪的效果。 There are a few options for this.为此,有几个选项。 You could just add double-quotes around the variable part:您可以在变量部分周围添加双引号:

sed -i '/group-title="'"${group2}"/',+1d' JWLINE.m3u

...but IMO this is confusing; ...但 IMO 这令人困惑; some of those quotes are syntactic (ie parsed by the shell), and one is literal (passed to sed as part of the regex), and it's not obvious which are which.其中一些引号是句法的(即由 shell 解析),一个是文字(作为正则表达式的一部分传递给sed ),并且不清楚哪些是哪些。 I'd prefer to just use double-quotes around the whole thing, and escape the double-quote that's supposed to be literal:我宁愿只在整个事情周围使用双引号,并避免应该是文字的双引号:

sed -i "/group-title=\"${group2}/,+1d" JWLINE.m3u
                     ^^ Escape makes this " a literal part of the argument.

(In double-quotes, you'd also need to escape any dollar signs, backslashes, or backticks that were supposed to be literal parts of the argument. But in this case, there aren't any of those.) (在双引号中,您还需要转义任何应该是参数字面部分的美元符号、反斜杠或反引号。但在这种情况下,没有任何这些。)

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

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