如何使用日志文件中的 shell 脚本选择单词、以特定单词开始和结束
How to pick words, start and end with specific words using shell script in a log file
我正在尝试选择以“Approved by”开头并以“=”符号结束的单词。这是日志文件。
Test worker] INFO cyyom.bghhht.gsghhj.dijjkkgital.dggcf.applicationservice.service.RequestServiceImpl - Approved List :
[AssignAccountApplicationRequestDto [status=true, requestId=1, backendData=BackendPopupDto [userID=0, companyId=0, ApplicationId=0,
userType=Test, corpId=0, revenueOwner=notnull, requestedDate=null]]] , Rejected List : [AssignAccountApplicationRequestDto [status=false, requestId=11, backendData=null]]
Approved by ha:////4P4ei7QWIY1VDT3ygY1geg0Q82Jj2AqLzGAAAAmh+LCAAAAAAAAP9b85aBtbiIQTGjNKU4P08vOT+vOD8nVc83PyU1x6OyILUoJzMv2y+/
JJUBAhiZGBgqihhk0NSjKDWzXb3RdlLBUSYGJk8GtpzUvPSSDB8G5tKinBIGIZ+sxLJE/ZzEvHT94JKizLx0a6BxUmjGOUNodHsLgAyxEgY+/
dLi1CL99OKCxJTczDwAUT2GdsMAAAA=PSPortal Administrator
我试过了
awk ' == "Approved" { print , }' log
那么结果是
ha:////4P4ei7QWIY1VDT3ygY1geg0Q82Jj2AqLzGAAAAmh+LCAAAAAAAAP9b85aBtbiIQTGjNKU4P08vOT+vOD8nVc83PyU1x6OyILUoJzMv2y+/JJUBAhiZGBgqihhk0NSjKDWzXb3RdlLBUSYGJk8GtpzUvPSSDB8G5tKinBIGIZ+sxLJE/ZzEvHT94JKizLx0a6BxUmjGOUNodHsLgAyxEgY+/dLi1CL99OKCxJTczDwAUT2GdsMAAAA=PSPortal Administrator
预期输出
PSPortal Administrator
使用您显示的示例,请尝试以下 awk
代码。考虑到该行以 Approved by
开头并且 =
出现在同一行然后执行以下操作。
awk -F'=' '/^Approved by / && NF>=2{print $NF}' Input_file
或如果你只有一个=
然后尝试以下:
awk -F'=' '/^Approved by / && NF==2{print $NF}' Input_file
解释: 简单的解释就是,将 Input_file 的所有行的字段分隔符设置为 =
.在主程序检查条件中,如果行从 Approved by
开始并且 NF
等于 2,则打印该行的最后一个字段。
我正在尝试选择以“Approved by”开头并以“=”符号结束的单词。这是日志文件。
Test worker] INFO cyyom.bghhht.gsghhj.dijjkkgital.dggcf.applicationservice.service.RequestServiceImpl - Approved List :
[AssignAccountApplicationRequestDto [status=true, requestId=1, backendData=BackendPopupDto [userID=0, companyId=0, ApplicationId=0,
userType=Test, corpId=0, revenueOwner=notnull, requestedDate=null]]] , Rejected List : [AssignAccountApplicationRequestDto [status=false, requestId=11, backendData=null]]
Approved by ha:////4P4ei7QWIY1VDT3ygY1geg0Q82Jj2AqLzGAAAAmh+LCAAAAAAAAP9b85aBtbiIQTGjNKU4P08vOT+vOD8nVc83PyU1x6OyILUoJzMv2y+/
JJUBAhiZGBgqihhk0NSjKDWzXb3RdlLBUSYGJk8GtpzUvPSSDB8G5tKinBIGIZ+sxLJE/ZzEvHT94JKizLx0a6BxUmjGOUNodHsLgAyxEgY+/
dLi1CL99OKCxJTczDwAUT2GdsMAAAA=PSPortal Administrator
我试过了
awk ' == "Approved" { print , }' log
那么结果是
ha:////4P4ei7QWIY1VDT3ygY1geg0Q82Jj2AqLzGAAAAmh+LCAAAAAAAAP9b85aBtbiIQTGjNKU4P08vOT+vOD8nVc83PyU1x6OyILUoJzMv2y+/JJUBAhiZGBgqihhk0NSjKDWzXb3RdlLBUSYGJk8GtpzUvPSSDB8G5tKinBIGIZ+sxLJE/ZzEvHT94JKizLx0a6BxUmjGOUNodHsLgAyxEgY+/dLi1CL99OKCxJTczDwAUT2GdsMAAAA=PSPortal Administrator
预期输出
PSPortal Administrator
使用您显示的示例,请尝试以下 awk
代码。考虑到该行以 Approved by
开头并且 =
出现在同一行然后执行以下操作。
awk -F'=' '/^Approved by / && NF>=2{print $NF}' Input_file
或如果你只有一个=
然后尝试以下:
awk -F'=' '/^Approved by / && NF==2{print $NF}' Input_file
解释: 简单的解释就是,将 Input_file 的所有行的字段分隔符设置为 =
.在主程序检查条件中,如果行从 Approved by
开始并且 NF
等于 2,则打印该行的最后一个字段。