Home > database >  How to pick words, start and end with specific words using shell script in a log file
How to pick words, start and end with specific words using shell script in a log file

Time:05-07

I am tring to pick words start with "Approved by" and end with before the "=" sign. here is log file.

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

I tried

awk '$1 == "Approved" { print $3, $4 }' log

then result was

ha:////4P4ei7QWIY1VDT3ygY1geg0Q82Jj2AqLzGAAAAmh LCAAAAAAAAP9b85aBtbiIQTGjNKU4P08vOT vOD8nVc83PyU1x6OyILUoJzMv2y /JJUBAhiZGBgqihhk0NSjKDWzXb3RdlLBUSYGJk8GtpzUvPSSDB8G5tKinBIGIZ sxLJE/ZzEvHT94JKizLx0a6BxUmjGOUNodHsLgAyxEgY /dLi1CL99OKCxJTczDwAUT2GdsMAAAA=PSPortal Administrator

expected output

PSPortal Administrator

CodePudding user response:

With your shown samples, please try following awk code. Considering that line starts with Approved by and = comes in same line then do following.

awk -F'=' '/^Approved by / && NF>=2{print $NF}'  Input_file

OR if you have only one = in line then try following:

awk -F'=' '/^Approved by / && NF==2{print $NF}'  Input_file

Explanation: Simple explanation would be, setting field separator as = for all lines of Input_file. In main program checking condition if line starts from Approved by and NF is equal to 2 then print last field of that line.

  • Related