【转】awk 里的substr函数用法举例

awk 里的substr函数用法举例:

要截取的内容:
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个字符结束.

转自:http://yhj1065.blog.163.com/blog/static/198002172007744121626/

上一篇:MYSQL-联合索引


下一篇:用C语言绘制一条标准的余弦曲线