標籤:shell 老男孩教育 企業面試
##########20150419##############################題目:變數a=‘a/b/c‘如何截取得到c @@@@@@@@@@@@@@@@來自營運班21期悠久之翼整理@@@@@@@@@@@@@@@@@@@[[email protected] shell]$ uname -r 2.6.32-504.el6.x86_64[[email protected] shell]$ cat /etc/redhat-release CentOS release 6.6 (Final)方法一[[email protected] shell]$ echo $a | cut -c5c方法二[[email protected] shell]$ echo $a | tr "a/b/" " " ##四個空格 c 方法三[[email protected] shell]$ echo $a | tr "/" "\n"|tail -1c方法四[[email protected] shell]$ echo $a | tr -d ‘a/b/‘c方法五[[email protected] shell]$ echo $a | rev |head -c1c[[email protected] shell]$ 方法六[[email protected] shell]$ echo $a | rev |cut -c1c方法七[[email protected] shell]$ echo $a | grep -o "[a-z]$"c方法八[[email protected] shell]$ echo $a | grep -o "[^ab/]"c方法九[[email protected] shell]$ echo $a | sed ‘s#.*/##‘ c方法十 [[email protected] shell]$ echo $a | sed ‘y#a/b/# #‘ ##四個空格 c方法十一[[email protected] shell]$ echo $a | sed -r ‘s#....(.)#\1#‘ c方法十二[[email protected] shell]$ echo $a | sed -r ‘s#.*([a-z]$)#\1#‘c方法十三[[email protected] shell]$ echo $a | awk -F/ ‘{print $NF}‘c方法十四[[email protected] shell]$ echo $a | awk ‘{gsub(".*/","",$0);print}‘c方法十五[[email protected] shell]$ echo $a | awk ‘{split($0,array,"/");print array[3]}‘c方法十六[[email protected] shell]$ echo $a | awk ‘{split($0,array,"a/b/");print array[2]}‘c方法十七[[email protected] shell]$ echo $a | awk ‘{sub(/[^c]+/,"");print}‘c方法十八[[email protected] shell]$ echo $a | awk ‘{gsub("/","\n");print $3}‘c方法十九[[email protected] shell]$ echo $a | awk ‘{print substr($0,5,1) }‘c方法二十[[email protected] shell]$ echo $a | awk ‘BEGIN{RS="[/\n]"}NR==3‘c方法二十一[[email protected] shell]$ echo $a|cut -d/ -f3 ##上海-龔小逸Nc方法二十二[[email protected] shell]$ echo ${a:4}##北京-徐富成c方法二十三[[email protected] shell]$ echo ${a##*/}##北京-徐富成c方法二十四[[email protected] shell]$ echo $a |tr "/" "\n"|awk ‘NR==3‘c方法二十五[[email protected] shell]$ echo ${a:4:1}##上海-龔小逸c方法二十六[[email protected] shell]$ echo ${a##a/b/} ##北京-劉曉濤c
linux shell取變數的子串26種方法實踐