【发布时间】:2018-05-22 20:20:00
【问题描述】:
我需要从日志文件中读取某些参数,然后更新到数据库。我正在尝试实现第一部分,即在 shell 脚本中使用 awk 命令从日志文件中读取
日志文件可能包含以下行或更多-
[2018-05-22T11:35:17,857] [RQST: rqst_3ADE-5439-598D-1B8B | TB: 9000042] - [588455375] - INFO - com.test.webapp.services.functions.TestTransactionService - Line 769 - requestType="TESTING",partnerName="Test Merchant 123",testId="123456",lob="TEST1_TO_TEST2",tranType="TEST1",paymentType="P2M",amount="110.00",currency="840",processor="CBN",network="TestSend",responseCode="00", acctNumLastFour="0087",binCountry="USA",binCurr="USD"
[2018-05-22T11:35:17,857] [RQST: rqst_2AEF-2339-598D-1B8B | TB: 9000043] - [588455376] - INFO - com.test.webapp.services.functions.TestTransactionService - Line 770 - requestType="TESTING",partnerName="Test Merchant 234",testId="234567",lob="TEST2_TO_TEST3",tranType="TEST2",paymentType="P2M",amount="120.00",currency="850",processor="CBN",network="TestSend",responseCode="00", acctNumLastFour="0087",binCountry="USA",binCurr="USD"
[2018-05-22T11:35:17,857] [RQST: rqst_4EDA-4539-598D-1B8B | TB: 9000044] - [588455377] - INFO - com.test.webapp.services.functions.TestTransactionService - Line 771 - requestType="TESTING",partnerName="Test Merchant 345",testId="345678",lob="TEST3_TO_TEST4",tranType="TEST3",paymentType="P2M",amount="130.00",currency="860",processor="CBN",network="TestSend",responseCode="00", acctNumLastFour="0087",binCountry="USA",binCurr="USD"
我需要应用过滤器处理器和支付类型,并将金额、货币、网络和响应代码的值检索到将插入到 Oracle 数据库表中的 shell 脚本中的变量。
我是 ShellScript 和 AWK 的新手,无法包装它。我试过使用
awk '/amount/{print}' testAPI.log
但是,返回所有有数量的行。
【问题讨论】:
-
你的日志真的是这样的吗?不要将其粘贴并格式化为“引用”,而是使用
{}按钮将其格式化为“代码”(或每行缩进 4 个空格。 -
您希望该命令做什么?
/amount/表示如果该行包含该字符串,它应该执行以下块。 -
如果您只想打印
amount="110.00"之类的内容,则需要将字段分隔符设置为,,然后使用for循环遍历字段。检查该字段是否匹配/^amount=/,是否打印该字段。 -
@Barmar - 我想从金额(也从问题中提到的其他字段)中检索值 110.00 以更新数据库表
-
因此,当您到达该字段时,使用
split()将其拆分为"字符,然后从数组的第二个元素中获取数字。