LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-13-2010, 03:08 AM   #1
srajeshkumar
LQ Newbie
 
Registered: Jul 2010
Posts: 22

Rep: Reputation: 0
Lightbulb I am requesting help related to the grep command


Hi All,

I would like to know how to use grep command to filter the log files created between 3:00 PM to 4:30 PM in buch of log for whole day in different headings. This files resembles like sar file in linux.
Please help with this query.

Thanks...
 
Old 12-13-2010, 03:12 AM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Please post some lines from the log files
 
Old 12-13-2010, 03:39 AM   #3
srajeshkumar
LQ Newbie
 
Registered: Jul 2010
Posts: 22

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by catkin View Post
Please post some lines from the log files


03:00:01 0 21.21 0.00 33.99 0.00 0.00 44.80
03:00:01 1 1.89 0.00 3.51 0.00 0.00 94.61
03:00:01 2 1.55 0.00 8.79 0.01 0.00 89.66
03:00:01 3 1.61 0.00 2.88 0.00 0.00 95.51
03:00:01 4 1.51 0.00 6.87 0.00 0.00 91.63
03:00:01 5 1.49 0.00 2.88 0.00 0.00 95.62
03:00:01 6 1.17 0.00 6.52 0.00 0.00 92.31
03:00:01 7 1.58 0.00 2.88 0.00 0.00 95.54
03:10:01 all 4.03 0.00 8.50 0.00 0.00 87.47
03:10:01 0 21.86 0.00 34.64 0.00 0.00 43.50
03:10:01 1 1.75 0.00 3.18 0.00 0.00 95.07
03:10:01 2 1.52 0.00 8.58 0.01 0.00 89.89
03:10:01 3 1.58 0.00 2.82 0.00 0.00 95.60
03:10:01 4 1.44 0.00 6.69 0.00 0.00 91.88
03:10:01 5 1.50 0.00 2.73 0.00 0.00 95.77
03:10:01 6 1.17 0.00 6.59 0.00 0.00 92.24
03:10:01 7 1.47 0.00 2.73 0.00 0.00 95.80
03:20:01 all 4.11 0.00 8.42 0.00 0.00 87.47
03:20:01 0 22.75 0.00 35.39 0.00 0.00 41.87
03:20:01 1 1.74 0.00 3.20 0.00 0.00 95.05
03:20:01 2 1.43 0.00 8.34 0.00 0.00 90.23
03:20:01 3 1.46 0.00 2.58 0.00 0.00 95.96
03:20:01 4 1.40 0.00 6.33 0.00 0.00 92.27


I have provided few lines from the log file like this we have many catogaries and each will be logged for the hole day..
 
Old 12-13-2010, 03:42 AM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Thanks for the extra information. sed and awk may be better tools for the job and some regular posters here are very good with those tools so I'll not write more until they have had a chance to respond. At least they will know the format of the files now.
 
1 members found this post helpful.
Old 12-13-2010, 04:14 AM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Quote:
Originally Posted by catkin View Post
Thanks for the extra information. sed and awk may be better tools for the job and some regular posters here are very good with those tools so I'll not write more until they have had a chance to respond. At least they will know the format of the files now.
Catkin, you're too humble!

@srajeshkumar: actually I will suggest a solution in bash, since the date command is more flexible than the time functions in awk. Here we go:
Code:
#!/bin/bash
initime=$(date -u -d "$1" +%s)
endtime=$(date -u -d "$2" +%s)
while read line
do
  timenow=$(echo "$line" | cut -d' ' -f1 | date -u -f- +%s)
  if [[ $timenow -ge $initime && $timenow -le $endtime ]]
  then
    echo "$line"
  fi
done < "$3"
where the positional parameters will be: start date, final date, log file name, e.g.
Code:
./script.sh "03:00 AM" "03:15 AM" file.log
the dates must be in any valid format recognized by the date command. See info date for more details.
 
Old 12-13-2010, 04:28 AM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
And here is a slightly faster solution using both shell and awk:
Code:
#!/bin/bash
initime=$(date -u -d "$1" +%s)
endtime=$(date -u -d "$2" +%s)
content=$(paste <(date -u -f <(cut -d' ' -f1 "$3") +%s) "$3")

echo "$content" | awk -v initime=$initime -v endtime=$endtime '
  
  $1 >= initime && $1 <= endtime {
  
     sub(/[0-9]+\t/,"")
     
     print
     
  }'

Last edited by colucix; 12-13-2010 at 04:30 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
grep related question... robertjinx Linux - Server 2 11-19-2010 06:52 PM
How to pass the result of a command to another command (like grep) desb01 Programming 4 06-25-2009 01:09 PM
Samba: net usershare command, and requesting an example from smb.conf DarkFlame Linux - Server 9 01-26-2009 11:01 AM
Grep related: checking for This AND That topcat Programming 3 04-01-2005 04:41 AM
grep and framebuffer errors... related? kodiakmook Linux - Software 2 01-29-2002 08:05 PM


All times are GMT -5. The time now is 01:16 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration