grep based on date


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting grep based on date
# 8  
Old 01-26-2009
simple approach -- take advantage of strings

Guys, we really don't need to do any date conversions at all.
A simple string comparison will work fine if the date is formatted correctly.

Here is a shell solution:

#----------------------------------------------------------------------#
# Date conversion. #
#----------------------------------------------------------------------#
convert_dt()
{

echo $1 $2 | sed -e 's/[\/:]/ /g' |
read mon mday year hour min sec ff

print "$year$mon$mday $hour$min$sec$ff"

}




#----------------------------------------------------------------------#
# Usage statement. #
#----------------------------------------------------------------------#
if [ $# -lt 3 ]; then
echo usage: $0 date time grep-string
echo date in mm/dd/yy format
echo time in hh:mm:ss:ff format
exit 1
fi



#----------------------------------------------------------------------#
# Set our variables. #
#----------------------------------------------------------------------#
input_dt="$1 $2"
shift
shift
grep_string="$*"


#----------------------------------------------------------------------#
# Convert our input date to a "number". #
#----------------------------------------------------------------------#
convert_dt $input_dt | read in_day in_time

#----------------------------------------------------------------------#
# Read the log -- converted the time stamp on each line. #
#----------------------------------------------------------------------#
cat your_file_here |
while read mmddyy hhmmssff stuff ; do

convert_dt $mmddyy $hhmmssff |
read file_day file_time

#----------------------------------------------------------------------#
# If the day is greater than or equal to, print the line. #
#----------------------------------------------------------------------#
if [ $file_day -ge $in_day ]; then
print $mmddyy $hhmmssff $stuff
fi

done |
#----------------------------------------------------------------------#
# ... and finally grep out the lines of interest. #
#----------------------------------------------------------------------#
grep $grep_string



Dang -- but the forum is mangling all my nice indentation . . . .
# 9  
Old 02-05-2009
The following worked for me. Thanks to all.

x=

x=$1"_"$2

nawk -v d="${x}" '{ if($1"_"$2 >= d) {print $1 " " $2 " " $3 " " $4 " " $5}}' /path/filename.log | grep -c FF
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Splitting week start date and end date based on custom period start dates

Below are my custom period start and end dates based on a calender, these dates are placed in a file, for each period i need to split into three weeks for each period row, example is given below. Could you please help out to achieve solution through shell script.. File content: ... (2 Replies)
Discussion started by: nani2019
2 Replies

2. HP-UX

HP/UX command to pull file name/date based on date

HI, Can anyone tell me how to pull the date and file name separated by a space using the find command or any other command. I want to look through several directories and based on a date timeframe (find -mtime -7), output the file name (without the path) and the date(in format mmddyyyy) to a... (2 Replies)
Discussion started by: lnemitz
2 Replies

3. UNIX for Dummies Questions & Answers

Find the count of files by last created date based on the given date range

My unix version is IBM AIX Version 6.1 I tried google my requirement and found the below answer, find . -newermt “2012-06-15 08:13" ! -newermt “2012-06-15 18:20" But newer command is not working in AIX version 6.1 unix I have given my requirement below: Input: atr files: ... (1 Reply)
Discussion started by: yuvaa27
1 Replies

4. Shell Programming and Scripting

Script to determine Date,TotalFile,total size of file based on date

I have file listed like below -rw-r--r--+ 1 test test 17M Nov 26 14:43 test1.gz -rw-r--r--+ 1 test test 0 Nov 26 14:44 test2.gz -rw-r--r--+ 1 test test 0 Nov 27 10:41 test3.gz -rw-r--r--+ 1 test test 244K Nov 27 10:41 test4.gz -rw-r--r--+ 1 test test 17M Nov 27 10:41 test5.gz I... (5 Replies)
Discussion started by: krish2014
5 Replies

5. UNIX for Dummies Questions & Answers

Copy log based on from-date and to-date

Hi all, i go a customer support requirement where i need to scan several files based on from/to date like 1-oct to 2-oct please help... (3 Replies)
Discussion started by: AbhiJ
3 Replies

6. UNIX for Dummies Questions & Answers

Condition based on Timestamp (Date/Time based) from logfile (Epoch seconds)

Below is the sample logfile: Userids Date Time acb Checkout time: 2013-11-20 17:00 axy Checkout time: 2013-11-22 12:00 der Checkout time: 2013-11-17 17:00 xyz Checkout time: 2013-11-19 16:00 ddd Checkout time: 2013-11-21 16:00 aaa Checkout... (9 Replies)
Discussion started by: asjaiswal
9 Replies

7. Shell Programming and Scripting

Calculating expiry date using date,sed,grep

Hi, I would greatly appreciate it if someone can help me with my problem. I have a crawler which collects spam URLs everyday & this data needs to be published in a blacklist. Here's the catch: The "Time To Live" (TTL) for each URL is 3 months (or whatever for that matter). If i see the... (5 Replies)
Discussion started by: r4v3n
5 Replies

8. Shell Programming and Scripting

Get the oldest date based on date in the filename

I am using ksh93 on Solaris. Ok, this may seem like a simple request at first. I have a directory that contains sets of files with a YYYYMMDD component to the name, along with other files of different filespecs. something like this: 20110501_1.dat 20110501_2.dat 20110501_3.dat... (2 Replies)
Discussion started by: gary_w
2 Replies

9. Shell Programming and Scripting

grep and cat based on specific mod date

I'm trying to figure out how to open and copy all contents of files last modded on aug 14 to one single text file. Also, I'm trying to do this in one command string. I have ls -l -R | grep "Aug 1" but all this does is print the -l info with Aug 1 in it. how can I modify this so that ls... (3 Replies)
Discussion started by: mmixology
3 Replies

10. Shell Programming and Scripting

grep latest file based on date.

hi all, not sure if this has been posted b4 but i try to search but not valid. this is my question: when i do a ls -ltr there will be a list generated as follows: -rw-r--r-- 1 root sys 923260 Jan 10 04:38 FilePolling.41025.083TL021.xml -rw-r--r-- 1 root sys 1761337 Jan 10 04:40... (12 Replies)
Discussion started by: lweegp
12 Replies
Login or Register to Ask a Question