[英]Shell Scripting | tr -d '\[\]\_'
I am working on a project, and wondering about this command in particular. 我正在做一个项目,尤其是想知道这个命令。
tr -d '\[\]\_'
I am aware that the -d flag deletes all characters matching the criteria of the regular expression inside, but I am unsure of what this expression is saying. 我知道-d标志删除了所有与正则表达式内部条件匹配的字符,但是我不确定该表达式在说什么。 Is it deleting all special characters such as '!"[]\\ and such? Or is that this command?
是否要删除所有特殊字符,例如'!“ [] \\等?还是该命令?
tr -cd "[[:alnum:]\n ]"
Thank you in advance! 先感谢您!
echo "][][_word[]_" | tr -d '\\[\\]\\_'
echo "][][_word[]_" | tr -d '\\[\\]\\_'
outputs word
echo "][][_word[]_" | tr -d '\\[\\]\\_'
输出word
The backslashes are escaping the brackets because they're special characters in regex expressions. 反斜杠转义了括号,因为它们是正则表达式中的特殊字符。 So this command is just deleting all
[
]
and _
characters from the string. 因此,该命令只是删除字符串中的所有
[
]
和_
字符。
However, tr -d '[]_'
is enough because tr does not use regex. 但是,
tr -d '[]_'
就足够了,因为tr不使用正则表达式。
tr -d '\\[\\]\\_'
removes square brackets and underscores from standard input. tr -d '\\[\\]\\_'
从标准输入中删除方括号和下划线。
Escaping [
, ]
and _
is useless here as these characters have no special meaning with tr
, they will always be handled as literals. 在此处转义
[
, ]
和_
是没有用的,因为这些字符在tr
没有特殊含义,它们将始终作为文字处理。
Note that tr
does not use regular expressions . 请注意,
tr
不使用正则表达式 。
From man page : 从手册页:
The format of the SET1 and SET2 arguments resembles the format of regular expressions;
SET1和SET2参数的格式类似于正则表达式的格式。 however, they are not regular expressions, only lists of characters.
但是,它们不是正则表达式,而只是字符列表。
[:alnum:]
matches alphanumeric characters (equivalent to 0-9A-Za-z
), so nothing to do with special characters. [:alnum:]
匹配字母数字字符(相当于0-9A-Za-z
),因此与特殊字符无关。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.