Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Selecting latest entry in the log file

Shell Programming and Scripting


Tags
find, grep, log extract, scripting, shell, solved, solved awk

Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 07-17-2017
simpsa27 simpsa27 is offline
Registered User
 
Join Date: Mar 2017
Last Activity: 16 August 2017, 4:21 AM EDT
Posts: 28
Thanks: 12
Thanked 0 Times in 0 Posts
Selecting latest entry in the log file

Hi there

I am trying to write a script where I will need to look for a specific word in the log file and I am aware this can be done by grep for example.

As there will be multiple entries for this I want to grep the last one to enter the log... how would I go about this - would I have to use tail?

I have attached an example below:

Code:
7/17/17 09:15:55.990 AM BST [INFO] [TimPollThreadPool.Thread1] [Manager.com.timestock.tess.services.tim.TimIo] File 'Wynyard_MTP_Primary-defect-15002793530000919233.xml' read, length=4231
7/17/17 09:15:56.005 AM BST [INFO] [TimPollThreadPool.Thread1] [Manager.com.timestock.tess.services.tim.TimIo] File 'Wynyard_MTP_Primary-login-15002793540000119307.xml-enc' read, length=1824
7/17/17 09:15:56.018 AM BST [INFO] [TimPollThreadPool.Thread1] [Manager.com.timestock.tess.services.tim.TimIo] File 'Wynyard_MTP_Primary-login-15002793540000121342.xml-enc' read, length=1056
7/17/17 09:15:56.024 AM BST [INFO] [TimPollThreadPool.Thread1] [Manager.com.timestock.tess.services.tim.TimIo] File 'Wynyard_MTP_Primary-login-15002793550000098410.xml-enc' read, length=1040
7/17/17 09:15:56.030 AM BST [INFO] [TimPollThreadPool.Thread1] [Manager.com.timestock.tess.services.tim.TimIo] File 'Wynyard_MTP_Primary-defect-15002793550000098410.xml' read, length=34131

As you can see their are multiple "login" & "defect" which is what I will need to be searching for how would I go about selecting the last or latest entry in the log file.

Thanks in advance

Alex
Sponsored Links
    #2  
Old Unix and Linux 07-17-2017
RavinderSingh13 RavinderSingh13 is online now Forum Advisor  
Registered User
 
Join Date: May 2013
Last Activity: 26 September 2017, 6:51 AM EDT
Location: Chennai
Posts: 2,609
Thanks: 573
Thanked 1,238 Times in 1,116 Posts
Hello simpsa27,

Could you please try following and let me know if this helps you.

Code:
grep "login" Input_file | tail -1

You could put any string there in place of login above, let me know if you have any queries on same.

Thanks,
R. Singh
The Following User Says Thank You to RavinderSingh13 For This Useful Post:
simpsa27 (07-17-2017)
Sponsored Links
    #3  
Old Unix and Linux 07-17-2017
simpsa27 simpsa27 is offline
Registered User
 
Join Date: Mar 2017
Last Activity: 16 August 2017, 4:21 AM EDT
Posts: 28
Thanks: 12
Thanked 0 Times in 0 Posts
Hi Ravinder

Yes I did the below command:


Code:
grep Primary-login /opt/ca/APM/Introscope10.2.0.27/logs/IntroscopeEnterpriseManager.log | tail -1 | awk '{print $9}'

This brings back what I expected:

Code:
'Wynyard_MTP_Primary-login-15002849400000527358.xml-enc'

I now want to grep again so I just get 15002849400000527358 number but only the first 10 so it would be 1500284940 is that possible?

Cheers
Alex
    #4  
Old Unix and Linux 07-17-2017
RavinderSingh13 RavinderSingh13 is online now Forum Advisor  
Registered User
 
Join Date: May 2013
Last Activity: 26 September 2017, 6:51 AM EDT
Location: Chennai
Posts: 2,609
Thanks: 573
Thanked 1,238 Times in 1,116 Posts
Hello simpsa27,

Could you please try following and let me know if this helps you.

Code:
awk --re-interval '/Primary-login/{val=$9} END{match(val,/-[0-9]{10}/);print substr(val,RSTART+1,RLENGTH-1)}'   Input_file

Thanks,
R. Singh
The Following User Says Thank You to RavinderSingh13 For This Useful Post:
simpsa27 (07-17-2017)
Sponsored Links
    #5  
Old Unix and Linux 07-17-2017
simpsa27 simpsa27 is offline
Registered User
 
Join Date: Mar 2017
Last Activity: 16 August 2017, 4:21 AM EDT
Posts: 28
Thanks: 12
Thanked 0 Times in 0 Posts
Thank you so much Ravinder!

That works just how I need it. Fantatsic.

Can I ask you to explain it so I can get a better understanding!

Cheers
Alex
Sponsored Links
    #6  
Old Unix and Linux 07-17-2017
RavinderSingh13 RavinderSingh13 is online now Forum Advisor  
Registered User
 
Join Date: May 2013
Last Activity: 26 September 2017, 6:51 AM EDT
Location: Chennai
Posts: 2,609
Thanks: 573
Thanked 1,238 Times in 1,116 Posts
Quote:
Originally Posted by simpsa27 View Post
Thank you so much Ravinder!
That works just how I need it. Fantatsic.
Can I ask you to explain it so I can get a better understanding!
Cheers
Alex
Hello simpsa27,

Your Welcome, happy to help, could you please go through following and let me know if this helps you.

Code:
awk --re-interval '/Primary-login/{                                         ##using --re-interval for using extended regex here, searching for string "Primary-login" in a line.
                                        val=$9                              ##creating a variable named val whose value is 9th field of that line.
                                  }
                                  END{                                      ##END block here of awk.
                                        match(val,/-[0-9]{10}/);            ##using match functionality here where matching 10 continuous digits in variable val which starts from -.
                                        print substr(val,RSTART+1,RLENGTH-1)##now printing the sub string of variable val so printing it from RSTART+1 to RLENGTH-1. So here RSTART and RLENGTH are variables which will be SET when a match happens for a regex.
                                     }                                      ##where RSTART will be starting index point of regex and RLENGTH is the length of that regex match.
                  '   Input_file

Thanks,
R. Singh

Last edited by RavinderSingh13; 07-17-2017 at 06:45 AM..
The Following User Says Thank You to RavinderSingh13 For This Useful Post:
simpsa27 (07-17-2017)
Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
List the file names available on FTP server before selecting the required file anuragpgtgerman Shell Programming and Scripting 1 03-02-2014 12:02 AM
Selecting the file of latest Date KAREENA18 UNIX for Dummies Questions & Answers 5 05-19-2012 11:21 AM
Crontab latest entry disappearing. plz help bluenavi Solaris 6 08-04-2011 05:12 PM
Crontab latest entry disappearing bluenavi Solaris 1 08-02-2011 08:05 AM
Removing duplicate rows & selecting only latest date shash UNIX for Dummies Questions & Answers 5 06-08-2011 11:34 AM



All times are GMT -4. The time now is 06:55 AM.