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',' '{print substr($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个字符开始,一直到设定的分隔.