LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   I am requesting help related to the grep command (https://www.linuxquestions.org/questions/linux-newbie-8/i-am-requesting-help-related-to-the-grep-command-849858/)

srajeshkumar 12-13-2010 02:08 AM

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...

catkin 12-13-2010 02:12 AM

Please post some lines from the log files

srajeshkumar 12-13-2010 02:39 AM

Quote:

Originally Posted by catkin (Post 4189476)
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..

catkin 12-13-2010 02:42 AM

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.

colucix 12-13-2010 03:14 AM

Quote:

Originally Posted by catkin (Post 4189497)
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.

colucix 12-13-2010 03:28 AM

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
   
  }'



All times are GMT -5. The time now is 12:09 PM.