awk字串截取

來源:互聯網
上載者:User

awk字串截取
1) 如何split當前的字串,用$0,例如:

cat num.2012032911 | awk '{print $2}' | awk '{split($0,b,".");print b[2]}' | cut -c 3-
擷取第二列,例如第二列是com.sb3456.you那麼在split當前的這個字串獲得sb3456,最後截取sb3456字串從第3個字元到最後的字元結果為456

b="c,d,e,f,g"
echo | awk '{split("'${b}'", array, ",");print array[1]}'
c

2) echo $b | cut -d "," -f 2
d
其中-d代表用什麼進行分割,-f2代表要第2個結果

3) 另外還有一個常用的參數是-c,用法為 -cx-y 這個意思是取得第x至第y個字串
echo “abcdefg” | cut -c3-6 輸出為 def

4) 格式化列

column –t 不過似乎對tab 和空格混合的情況無法處理

5)substr用法

string=/home/bozo/daily-journal.txt

echo | awk '{print substr("'${string}'",2,4)}'
// 從第2個字元開始擷取4個字元 結果為:home

6) 計算字串長度
echo | awk '{print length("'${string}'")}'
// 結果為: 29

7) 字串大小寫轉換
echo | awk '{ print toupper("test"), tolower("TEST") }'
結果為: TEST test

聯繫我們

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