2

I want to extract infromation from logs using a shell script for a particular time range . A line in logs looks like this:

[14:58:27:176][15-10-2015]: testing1-vidya-Thu Oct 15 11:49:27 IST 2015
[14:59:27:178][15-10-2015]: testing2-vidya-Thu Oct 15 11:49:27 IST 2015
[15:00:27:179][15-10-2015]: testing3-vidya-Thu Oct 15 11:49:27 IST 2015
[15:01:27:180][15-10-2015]: testing4-vidya-Thu Oct 15 11:49:27 IST 2015
[15:01:27:181][15-10-2015]: testing5-vidya-Thu Oct 15 11:49:27 IST 2015
[15:02:27:182][15-10-2015]: testing6-vidya-Thu Oct 15 11:49:27 IST 2015
[15:02:27:183][15-10-2015]: testing7-vidya-Thu Oct 15 11:49:27 IST 2015
[15:02:27:184][15-10-2015]: testing8-vidya-Thu Oct 15 11:49:27 IST 2015
[15:02:27:191][15-10-2015]: testing9-vidya-Thu Oct 15 11:49:27 IST 2015
[15:03:27:192][15-10-2015]: testing10-vidya-Thu Oct 15 11:49:27 IST 2015
[15:03:27:193][15-10-2015]: testing11-vidya-Thu Oct 15 11:49:27 IST 2015
[15:03:27:208][15-10-2015]: testing12-vidya-Thu Oct 15 11:49:27 IST 2015
[15:04:27:209][15-10-2015]: testing13-vidya-Thu Oct 15 11:49:27 IST 2015
[15:49:27:210][15-10-2015]: testing14-vidya-Thu Oct 15 11:49:27 IST 2015
[13:02:17:209][15-10-2015]: testing13-vidya-Thu Oct 15 11:49:27 IST 2015
[15:04:27:209][16-10-2015]: testing13-vidya-Fri Oct 16 11:49:27 IST 2015
[15:49:27:210][16-10-2015]: testing14-vidya-Fri Oct 16 11:49:27 IST 2015

I need to extract data in specific intervals. for example in between 14:59 to 15:03 on 15-10-2015 as shown below:

[14:59:27:178][15-10-2015]: testing2-vidya-Thu Oct 15 11:49:27 IST 2015
[15:00:27:179][15-10-2015]: testing3-vidya-Thu Oct 15 11:49:27 IST 2015
[15:01:27:180][15-10-2015]: testing4-vidya-Thu Oct 15 11:49:27 IST 2015
[15:01:27:181][15-10-2015]: testing5-vidya-Thu Oct 15 11:49:27 IST 2015
[15:02:27:182][15-10-2015]: testing6-vidya-Thu Oct 15 11:49:27 IST 2015
[15:02:27:183][15-10-2015]: testing7-vidya-Thu Oct 15 11:49:27 IST 2015
[15:02:27:184][15-10-2015]: testing8-vidya-Thu Oct 15 11:49:27 IST 2015
[15:02:27:191][15-10-2015]: testing9-vidya-Thu Oct 15 11:49:27 IST 2015
[15:03:27:192][15-10-2015]: testing10-vidya-Thu Oct 15 11:49:27 IST 2015
[15:03:27:193][15-10-2015]: testing11-vidya-Thu Oct 15 11:49:27 IST 2015
[15:03:27:208][15-10-2015]: testing12-vidya-Thu Oct 15 11:49:27 IST 2015

I am new to shell script.I have tried with grep command . Can anyone suggest me how to do this ?

4

You could use sed.

sed -n '/\[14:59/,/\[15:03/p' log

Update: remote via ssh

ssh user@remotehost.com "sed -n -e '/\[14:59/,/\[15:03/p' /var/logfile"

To only extract lines that were found between the times of interest on a specific date, include the date in your search string:

sed -n '/\[14:59.*15-10-2015*/,/\[15:03.*15-10-2015/p' log
12
  • @X Tian : sed: -e expression #1, char 18: unterminated address regex error is coming – Vidya Oct 15 '15 at 14:40
  • sshpass -p xxx ssh -o StrictHostKeyChecking=no xxx@HostIP 'cd Vidya;sed -n '/[14:59/,/[15:03/p'serverout >>result' – Vidya Oct 15 '15 at 14:42
  • If I executed separately it was wrking fine . But If I gave as above it is not working . Can you suggest me how to solve this ? – Vidya Oct 15 '15 at 14:45
  • 1
    your single-quote marks are getting mixed up – Jeff Schaller Oct 15 '15 at 15:05
  • Updated ans was helpful . Thank you for the suggestions. – Vidya Oct 15 '15 at 15:38
2

Grep which can use regular expressions, like this:

grep -E "^\[(14:59|15:00|15:01|15:02|15:03)" /path/to/file
3
  • 2
    Or, more concisely grep -E "^\[(14:59|15:0[0-3])" – glenn jackman Oct 15 '15 at 14:32
  • yes this is working fine. Thank you so much. – Vidya Oct 15 '15 at 14:49
  • @Vidya If one of these answers solved your issue, please take a moment and accept it by clicking on the check mark to the left. That will mark the question as answered and is the way thanks are expressed on the Stack Exchange sites. – terdon Oct 19 '15 at 10:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.