Retrieve lines from a file in a given date range


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Retrieve lines from a file in a given date range
# 1  
Old 05-04-2011
Retrieve lines from a file in a given date range

Hey, guys!

I am trying to retrieve lines from a file in a given date range. I tried using sed -n "/${SDATE}/,/${EDATE}/p" ~/webhits/$FILE | wc -l but that doesn't work if the starting or the end date do not match exactly. If both dates match, there are no problems (for example 25 March 2008 - 07 May 2008). But sometimes a file may not have an entry on 25 March or 07 May and then I get some weird results. Could you help me solve this?

Or maybe you can give me a solution using awk?

A file contains lines like:
Code:
116.46.193.60      Thu May 08 07:27:33 GMT 2008
57.193.63.184      Thu May 08 13:35:20 GMT 2008
79.133.218.14      Thu May 08 17:30:46 GMT 2008
79.133.218.14      Thu May 08 18:35:37 GMT 2008
30.8.24.200        Thu May 08 19:57:21 GMT 2008

I need to get the IP addresses that have been logged in a given date range.

cheers!

Last edited by joeyg; 05-04-2011 at 04:28 PM.. Reason: Please wrap program scripts and data with CodeTags.
# 2  
Old 05-04-2011
Code:
#!/bin/ksh93

startdate=$( printf "%(%s)T" "$1")
enddate=$( printf "%(%s)T" "$2")

while read ip datestr
do
  utcdate=$( printf "%(%s)T" "$datestr" )
  if (( startdate <= utcdate && utcdate <= enddate ))
  then
      echo "$ip  $datestr"
  fi
done < infile

Code:
$ ./demo "Thu May 08 07:27:33 GMT 2008"  "Thu May 08 18:35:37 GMT 2008"
116.46.193.60  Thu May 08 07:27:33 GMT 2008
57.193.63.184  Thu May 08 13:35:20 GMT 2008
79.133.218.14  Thu May 08 17:30:46 GMT 2008
79.133.218.14  Thu May 08 18:35:37 GMT 2008

# 3  
Old 05-05-2011
Thanks for the reply, but could you give some comments as to clarify the code a bit. I am fairly new to Unix shell programming.
Also, could you give a solutions for bash?
Thanks!
# 4  
Old 05-08-2011
please help..
# 5  
Old 05-08-2011
Try this code...
Code:
#!/bin/bash
d1=$(date -d "$1" '+%s'); d2=$(date -d "$2" '+%s')
while read ip dd; do
  dd=$(date -d "$dd" '+%s')
  if [ $dd -ge $d1 ] && [ $dd -le $d2 ]
  then
     echo $ip
  fi
done < file

Usage : run date1 date2
where date2 > date1
$ ./run "Thu May 08 07:27:33 GMT 2008" "Thu May 08 18:35:37 GMT 2008"

regards,
Ahamed

Last edited by ahamed101; 05-08-2011 at 11:58 PM..
# 6  
Old 05-10-2011
Thanks a lot, Ahamed! That was exactly what I needed! ^_^
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to search a text in file and retrieve required lines following it with UNIX command?

I have requirement to search for a text in the file and retrieve required lines that is user defined with unix command. Eg: Find the text UNIX in the below file and need to return Test 8 & Test 9 Test 1 Test 2 Test 3 Test 4 UNIX Test 5 Test 6 Test 7 Test 8 Test 9 Result can... (8 Replies)
Discussion started by: Arunkumarsak4
8 Replies

2. UNIX for Beginners Questions & Answers

How bash treats literal date value and retrieve year, month and date?

Hi, I am trying to add few (say 3 days) to sysdate using - date -d '+ 3 days' +%y%m%d and it works as expected. But how to add few (say 3 days) to a literal date value and how bash treats a literal value as a date. Can we say just like in ORACLE TO_DATE that my given literal date value... (2 Replies)
Discussion started by: pointers1234
2 Replies

3. Shell Programming and Scripting

Display lines of two date range from syslog file

Hi Guys, I want to display lines from Solaris syslog file but with 2 dates range. I have some similar solution (https://www.unix.com/shell-programming-scripting/39293-grep-log-file-between-2-dates-4.html) which works fine but as you know syslog has different date format (Jan 22) so this is not... (1 Reply)
Discussion started by: prashant2507198
1 Replies

4. Shell Programming and Scripting

Extracting specific lines of data from a file and related lines of data based on a grep value range?

Hi, I have one file, say file 1, that has data like below where 19900107 is the date, 19900107 12 144 129 0.7380047 19900108 12 168 129 0.3149017 19900109 12 192 129 3.2766666E-02 ... (3 Replies)
Discussion started by: Wynner
3 Replies

5. Programming

How can i retrieve some specific lines from a file using C

Plz tel me how to retrieve some specific set of lines from a file and store it in a char buffer.I am seperating each record by ":" 22:abc:4 hardware:cd:xyz:2 hardware:eth:abc:6 hardware:mouse:xyz:3 hardware:ram:xyz:1 23:cde:3 hardware:cd:xyz:2 hardware:eth:abc:6 hardware:ram:xyz:1 ... (3 Replies)
Discussion started by: vigneshinbox
3 Replies

6. Shell Programming and Scripting

display the file with in the date range

Hi All, I want a shell script which can display the file with in the date range. For Example I have 15 files with the following format abc_01-01-2009.txt to abc_15-01-2009.txt. Now I want to have the files between 4th of jan to 12th files. How can I acheive this. Advance... (1 Reply)
Discussion started by: fareed_do
1 Replies

7. Shell Programming and Scripting

retrieve lines from file which fall under the given date range

Hi, I need to retrieve the lines which fall under the given date range. eg:In a log file,i have the lines which will have the timestamp. the input will be some date range.eg: from date:03/Jan/2008,to date:24/Jul/2008.so now i want to retrieve the lines which have the timestamp between these... (5 Replies)
Discussion started by: Sharmila_P
5 Replies

8. UNIX for Dummies Questions & Answers

get Message from file within date range

Hi All, I am a java devloper putting my hands on shell scripts. Honestly it sounds coool and interesting Presently I got stuck with the following requirement. Get the messages from file. The messages in the file are as follows: date|message1 date|message2 . . . date is of... (2 Replies)
Discussion started by: ambharish
2 Replies

9. Shell Programming and Scripting

how to extract a range of lines from a file

I am reading a file that contains over 5000 lines and I want to assign it to a shell variable array (which has a restriction of 1024 rows). I had an idea that if I could grab 1000 record hunks of the file, and pipe the records out, that I could perform a loop until I got to the end and process 1000... (5 Replies)
Discussion started by: beilstwh
5 Replies

10. UNIX for Dummies Questions & Answers

Need to print file names in a certain date range using ls

I need to list only file names of a specific date from the command ls -lt. :confused: (2 Replies)
Discussion started by: Shamwari
2 Replies
Login or Register to Ask a Question