linux shell取變數的子串26種方法實踐

來源:互聯網
上載者:User

標籤: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種方法實踐

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.