vieyahn2017 / shellv

shell command test and study
4 stars 1 forks source link

9.3 shell 字符串匹配变量(只取数字或者取固定字符串) tr -cd "[0-9]" #71

Open vieyahn2017 opened 4 years ago

vieyahn2017 commented 4 years ago

shell 字符串匹配变量(只取数字或者取固定字符串)

https://www.cnblogs.com/ivyharding/p/11227245.html

vieyahn2017 commented 4 years ago

var1=abc3559 #想要获得3559

操作:

var1_key=`echo $var1 | tr -cd "[0-9]"`
vieyahn2017 commented 4 years ago

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 sequence of a repeated character that is listed in the last specified SET, 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

vieyahn2017 commented 4 years ago

var2=efg010B0C0 #efg是固定字符串,想要获得010B0C0

操作:

var2_key=echo $var2 | sed 's/sdk//g'

方法2:

var2_key=echo $var2 | tr -d "sdk"

vieyahn2017 commented 4 years ago

-c 使用指定内容的补集 -d 删除字符 -s 压缩字符 -t 截断字符