AWK操作字串的截取

來源:互聯網
上載者:User

AWK操作字串的截取 對於awk和Bash來說,他們使用的是不同的string索引系統;bash的第一個字元從0開始記錄;awk的第一個字元從1開始記錄;#012345678   ------------Bash#123456789  -------------Awk以下是案例說明:[html] [root@Slave02 shell]# vi substring-extraction.sh  #!/bin/bash    String=23skidoo1    echo ${String:2:4}  echo |awk '{ print substr("'"${String}"'",3,4) }'    exit 0  ~  ~  ~  ~  ~  ~  ~  ~  ~  ~  ~  ~  ~  ~  ~  "substring-extraction.sh" [New] 8L, 108C written  [root@Slave02 shell]# sh substring-extraction.sh   skid  skid  [root@Slave02 shell]#  使用變數的首碼來匹配前面聲明過的所有變數;如:[html] [root@Slave02 shell]# xyz23=watever  [root@Slave02 shell]# xyz24=asdf  [root@Slave02 shell]# echo "  a=${!xyz*}"  a=xyz23 xyz24  [root@Slave02 shell]# echo "a=${!xyz@}"  a=xyz23 xyz24  [root@Slave02 shell]#    拋骰子遊戲;SPOTS=6      -----模數為6,範圍在0-5die1=0die2=0 ------------2個變數名稱,保證每個平面選擇的數字記錄相同 let "die1 = $RANDOM % $SPOTS +1"let "die2 = $RANDOM % $SPOTS +1" let "throw = $die1 + $die2" echo "Throw of the dice = $throw"echo  exit 0 以下驗證結果:[html] Random number greater than 200 --- 25552    Throw of the dice = 5    [root@Slave02 shell]#   [html] random number less than 500 --- 0    Random number greater than 200 --- 9765    Throw of the dice = 2    [root@Slave02 shell]#   [html] Random number greater than 200 --- 31180    Throw of the dice = 10    [root@Slave02 shell]#    [root@Slave02 shell]# sh random2.sh Random number between 0 and 1 = 0.246062[root@Slave02 shell]# sh random2.sh Random number between 0 and 1 = 0.619153[root@Slave02 shell]# sh random2.sh Random number between 0 and 1 = 0.619153[root@Slave02 shell]# sh random2.sh Random number between 0 and 1 = 0.619153[root@Slave02 shell]# sh random2.sh Random number between 0 and 1 = 0.619153[root@Slave02 shell]# sh random2.sh Random number between 0 and 1 = 0.201116[root@Slave02 shell]# cat random2.sh #!/bin/bash  AWKSCRIPT='{ srand(); print rand() }'    -srand中偽隨機的計算  echo -n "Random number between 0 and 1 = "echo | awk "$AWKSCRIPT" exit 0[root@Slave02 shell]# 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.