awk substr()函數

來源:互聯網
上載者:User

awk 裡的substr函數用法舉例:

要截取要截取的內容1:

F115!16201!1174113017250745 10.86.96.41 211.140.16.1 200703180718

F125!16202!1174113327151715 10.86.96.42 211.140.16.2 200703180728

F235!16203!1174113737250745 10.86.96.43 211.140.16.3 200703180738

F245!16204!1174113847250745 10.86.96.44 211.140.16.4 200703180748

F355!16205!1174115827252725 10.86.96.45 211.140.16.5 200703180758

使用 awk 內建的取子串函數提取輸入檔案中的手機號

$ awk -F '[ !]' '{print substr($3,6)}' test.txt

13017250745

13327151715

13737250745

13847250745

15827252725

要截取的內容2:

2007-08-04 04:45:03.084 - SuccessfulTradeResult(status: 1, currencyPair: 'USDJPY', tradeId: '17389681', clientReference: '20070803COVR00013176', tradeDateTime: '2007-08-03T19:45:02', dealerUserId: 'PANTARHEI.API1', clientName: 'PANTA RHEI SECURITIES CO LTD
', clientId: 'EU0271383', counterpartyName: 'DB', buySell: 'S', nearLeftAmount: 1810000.0, nearRightAmount: 2.138696E8, nearRate: 118.16, nearValueDate: '2007-08-07')



操作指令:
 grep -v 'errorMessage' ./GWDBCpTradeResponse.20070803 | awk -F',' '{printsubstr($4,20)","substr($3,12,8)","substr($2,17,6)","substr($5,18,19)","substr($9,21,2)","substr($10,12,1)","substr($11,18)","substr($12,19)","substr($13,12)","substr($14,18,10)}'
| tr -d "'"



截取結果:

20070803COVR00013176,17389681,USDJPY,2007-08-3T19:45:02,DB,S,1810000.0,2.138696E8,118.16,2007-08-07


解釋:

substr($4,20)     --->  表示是從第4個欄位裡的第20個字元開始,一直到設定的分隔字元","結束.

substr($3,12,8)  --->  表示是從第3個欄位裡的第12個字元開始,截取8個字元結束.

substr($3,6)     --->  表示是從第3個欄位裡的第6個字元開始,一直到設定的分隔.

聯繫我們

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