Selecting latest entry in the log file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Selecting latest entry in the log file
# 1  
Old 07-17-2017
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
# 2  
Old 07-17-2017
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
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 07-17-2017
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 07-17-2017
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
This User Gave Thanks to RavinderSingh13 For This Post:
# 5  
Old 07-17-2017
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
# 6  
Old 07-17-2017
Quote:
Originally Posted by simpsa27
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 07:45 AM..
This User Gave Thanks to RavinderSingh13 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Monitor and capture the latest entry from the log file

Hi, I want to monitor a log file using tail -f command and search for a specific string on the most recent entry from the file. If the search string matches with the most recent or last line from the file, I want send an email to the people with the message. tail -f service.log|tail -n 1 ... (5 Replies)
Discussion started by: svajhala
5 Replies

2. UNIX for Beginners Questions & Answers

Selecting specific variable in log file

Hi there I am trying 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? Thanks in advance Alex (4 Replies)
Discussion started by: simpsa27
4 Replies

3. Shell Programming and Scripting

Need to grep certain entry out of log file

This one is a bit too challenging for me... Hopefully you guys can help. Let's say I have a log file called: "$MW_HOME/user_projects/domains/IDMDomain/servers/wls_ods?/logs/wls_ods1-diagnostic.log" In this log file I want to search for "DIP-10219". When I execute this $ cat... (7 Replies)
Discussion started by: exm
7 Replies

4. UNIX for Dummies Questions & Answers

Selecting the file of latest Date

Hi Folks, I have one query that there is a folder in which daily several logs files are getting created , I reached to that location through putty but what I observer that 10 files of different date are been created with same name , what I need to see is the latest file ...let say the location is ... (5 Replies)
Discussion started by: KAREENA18
5 Replies

5. Solaris

Crontab latest entry disappearing. plz help

The latest crontab entry is disappearing time and again on acceptance and production environment. the same entry gets deleted. any pointers to what might be causing this issue? (6 Replies)
Discussion started by: bluenavi
6 Replies

6. Solaris

Crontab latest entry disappearing

The latest crontab entry is disappearing time and again on acceptance and production environment. the same entry gets deleted. any pointers to what might be causing this issue? (1 Reply)
Discussion started by: bluenavi
1 Replies

7. UNIX for Dummies Questions & Answers

Removing duplicate rows & selecting only latest date

Gurus, From a file I need to remove duplicate rows based on the first column data but also we need to consider a date column where we need to keep the latest date (13th column). Ex: Input File: Output File: I know how to take out the duplicates but I couldn't figure out... (5 Replies)
Discussion started by: shash
5 Replies

8. Shell Programming and Scripting

Perl's buffered I/O is causing me to miss latest log file entries in log colorizer. How to fix?

I've been finding myself using a log file colorizer written in perl to reformat and colorize the output from many different programs. Mainly, however, I use it to make the output from "tail -f" commands more readable. The base perl script I use is based on "colorlogs.pl" available from the... (1 Reply)
Discussion started by: rcsteiner
1 Replies

9. Shell Programming and Scripting

how can i pick the latest log file as per below

in the below .. i want to pick the latest logfile which is having JPS.PR inside.. that means i want particularly "spgport040408041223.log:@@@@@@@@ 04:13:09 Adding: JPS.PR." which is latest among these.. is it possible to compare the current time with logfile time ? reptm@xblr0758rop>... (4 Replies)
Discussion started by: mail2sant
4 Replies

10. UNIX for Dummies Questions & Answers

latest status of an field in a log file

Hi I have got a log file which is like: id status --------------------- 12345 status1.true 12345 status2.true 12345 status3.false 12345 status4.true 23452 status5.true 23452 status6.true 23452 status7.false 65243 ... (2 Replies)
Discussion started by: encrypted
2 Replies
Login or Register to Ask a Question