语法:
# tr --help

Usage: tr [OPTION]... SET1 [SET2]
Translate, squeeze, and/or delete characters from standard input, writing to standard output.

-c, -C, --complement use the complement of SET1
-d, --delete delete characters in SET1, do not translate
-s, --squeeze-repeats replace each input sequence of a repeated character that is listed in SET1 with a single occurrence of that character
-t, --truncate-set1 first truncate SET1 to length of SET2
--help display this help and exit
--version output version information and exit

SETs are specified as strings of characters. Most represent themselves.
Interpreted sequences are:

\NNN character with octal value NNN (1 to 3 octal digits)
\\ backslash
\a audible BEL
\b backspace
\f form feed
\n new line
\r return
\t horizontal tab
\v vertical tab
CHAR1-CHAR2 all characters from CHAR1 to CHAR2 in ascending order
[CHAR*] in SET2, copies of CHAR until length of SET1
[CHAR*REPEAT] REPEAT copies of CHAR, REPEAT octal if starting with 0
[:alnum:] all letters and digits
[:alpha:] all letters
[:blank:] all horizontal whitespace
[:cntrl:] all control characters
[:digit:] all digits
[:graph:] all printable characters, not including space
[:lower:] all lower case letters
[:print:] all printable characters, including space
[:punct:] all punctuation characters
[:space:] all horizontal or vertical whitespace
[:upper:] all upper case letters
[:xdigit:] all hexadecimal digits
[=CHAR=] all characters which are equivalent to CHAR

Translation occurs if -d is not given and both SET1 and SET2 appear.
-t may be used only when translating. SET2 is extended to length of SET1 by repeating its last character as necessary. Excess characters of SET2 are ignored. Only [:lower:] and [:upper:] are guaranteed to expand in ascending order; used in SET2 while translating, they may only be used in pairs to specify case conversion. -s uses SET1 if not translating nor deleting; else squeezing uses SET2 and occurs after translation or deletion.

GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
For complete documentation, run: info coreutils 'tr invocation'

示例1:

echo "ocmao134h008dqaw" | tr -cd '[:digit:]'   # 提取纯数字字符部分
echo "ocmao134h00#_8dq/aw" | tr -cd '[:alnum:]'   # 提取非特殊字符部分
echo "ocmao134h00#_8dq/aw" | tr -cd '[:digit:]a-d'   # 提取纯数字字符和指定字符部分
echo "ocmao134h00#_8dq/aw" | tr -cd '[:digit:][a,b,o]'   # 提取纯数字字符和指定字符部分

示例2:

echo ${str} | tr "[:lower:]" "[:upper:]"  # 将变量 str 中的英文字符从小写转换成大写,然后输出转换后的完整字符串

$ str="14afpcpaRIM44" && echo ${str} |tr "[:lower:]" "[:upper:]"
14AFPCPARIM44

 

正则表达式,要匹配任意数字为什么要 [[:digit:]] 两个括号才行,一个括号就不对
[:digit:]就不对

具体官网的解释是:
These are always used inside square brackets in the form [[:alnum:]] or combined as [[:digit:]a-d]
via :http://www.zytrax.com/tech/web/regex.htm
即:
对于
[:digit:] Only the digits 0 to 9
[:alnum:] Any alphanumeric character 0 to 9 OR A to Z or a to z.
之类的含义,都是必须写成:
[xxx]
即:
[[:digit:]]
[[:alnum:]]
即,语法就是这么定义的。
自己用google搜:
Regular Expressions - A Gentle User Guide and Tutorial

posted on 2021-08-05 16:47  51core  阅读(81)  评论(0)    收藏  举报