######### #20150419 ############################# #题目: Variable a= ' a/b/c ' how to intercept get c @@@@@@@@@@@@@@@@ From the operation and maintenance class 21 long wing finishing @@@@@@@@@@@@@@@@@@@[[email protected] shell]$ uname -r 2.6.32-504. El6.x86_64[[email protected] shell]$ cat /etc/redhat-release centos release 6.6 (Final) method one [[email protected] shell]$ echo $a | cut -c5c method two [[ email protected] shell]$ echo $a | tr "a/b/" " " # #四个空格 c method Three [[email protected] shell]$ echo $a | tr "/" "\ n" |tail -1c method four [[Email protected] shell ]$ echo $a | tr -d ' a/b/' C method five [[Email protected] shell]$ echo $a | rev |head -c1c[[email protected] shell]$ method six [[email protected] shell]$ echo $a | rEv |cut -c1c method Seven [[email protected] shell]$ echo $a | grep -o "[a-z]$" C method eight [[email protected] shell]$ echo $a | grep -o "[^ab/]" C Method Nine [ [email protected] shell]$ echo $a | sed ' s#.*/## ' c method 10 [[ email protected] shell]$ echo $a | sed ' y#a/b/# # ' # #四个空格 c Method 11 [[email protected] shell]$ echo $a | sed -r ' s# .... (.) #\1# ' c method 12 [[email protected] shell]$ echo $a | sed -r ' s#.* ([A-Z ]$) #\1# ' C method 13 [[email protected] shell]$ echo $a | awk -F/ ' {print $NF} ' C method 14 [[email protected] shell]$ echo $a | awk ' {gsub (". * *", "", $ );p Rint} ' C method 15 [[email protected] shell]$ echo $a | awk ' {split ($0,array, "/");p Rint array[3]} ' C method 16 [[email protected] shell]$ echo $a | awk ' {split ($0,array, "a/b/");p rint array[2]} ' C method 17 [[email protected] shell]$ echo $ a | awk ' {sub (/[^c]+/, "");p Rint} ' C method 18 [[email protected] shell]$ echo $a | awk ' {gsub ("/", "\ n");p rint $3} ' C method 19 [[Email protected] shell]$ echo $a | awk ' {print substr ($0,5,1) } ' C method 20 [[email protected] shell]$ echo $a | awk ' begin{rs= ' [/\n] "}nr==3 ' C method 21 [[Email protected] shell ]$ echo $a |cut -d/ -f3 # #上海-Yi Xiaoyi nc method 22 [[Email protected] shell] $ echo ${a:4}# #北京-Xu Fu C method 23 [[email protected] shell]$ echo ${a##*/}## BEIJING-Xu Fu C method 24 [[email protected] shell]$ echo $a |tr "/" "\ n" |awk ' nr==3 ' C method 25 [[EMAIL PROTECTED]&NBsp;shell]$ echo ${a:4:1}# #上海-Yi Xiaoyi C Method 26 [[Email protected] shell]$ echo ${a# #a/b/} # #北京-Liu Xiaotao C
26 Ways to practice the substring of a Linux shell by taking variables