fetch data between two timestamp using script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting fetch data between two timestamp using script
# 1  
Old 12-29-2011
MySQL fetch data between two timestamp using script

Hi Guys,

I have the data in below format.

25 Dec 2011 03:00:01 : aaaaaaaaaaaaaaa
25 Dec 2011 04:23:23 : bbbbbbbbbbbbbbb
25 Dec 2011 16:12:45 : ccccccccccccccc
26 Dec 2011 04:45:34 : ddddddddddddddd
26 Dec 2011 17:01:22 : eeeeeeeeeeeeeee
27 Dec 2011 12:33:45 : ffffffffffffffffffffffff
28 Dec 2011 14:23:23 : ggggggggggggggg

Now i want to fetch the complete data between the two dates
25 Dec 2011 03:00:01 and 27 Dec 2011 12:33:45.

Which command to use.
I tried sed but that is very case sensitive and not giving the output properly.

Please help.
Thanks in advance.
# 2  
Old 12-29-2011
Welcome to the forum.

Try,

Code:
awk '/25 Dec 2011 03:00:01/,/27 Dec 2011 12:33:45/ {print $0}' file

Even shorter,

Code:
awk '/25 Dec 2011 03:00:01/,/27 Dec 2011 12:33:45/' file

# 3  
Old 12-29-2011
Hey Anchal,
Thanks for the command, but there seems to be some problem.

If i try a
Code:
awk '/25 Dec 2011 02:59:59/,/27 Dec 2011 12:33:45/' file

instead of
Code:
awk '/25 Dec 2011 03:00:01/,/27 Dec 2011 12:33:45/' file, it doesn't work.

I want to fetch data between two particular date with timestamp.

Last edited by Franklin52; 12-30-2011 at 08:56 AM.. Reason: Please use code tags for data and code samples, thank you
# 4  
Old 12-29-2011
perl code:
  1. #! /usr/bin/perl -w
  2. use strict;
  3.  
  4. my ($start_time, $end_time, $dt);
  5.  
  6. (@ARGV != 2) && die "Invalid parameters. Exiting";
  7.  
  8. $start_time  = parse_date ($ARGV[0]);
  9. $end_time    = parse_date ($ARGV[1]);
  10.  
  11. open I, "< inputfile.txt";
  12. for (<I>) {
  13.     $dt = parse_date (substr ($_, 0, 20));
  14.     ($dt >= $start_time && $dt <= $end_time) && print;
  15. }
  16.  
  17. sub parse_date {
  18.     my $t = shift;
  19.     my @d = split /\s+/, $t;
  20.     my %mnths = (   "Jan" => "01", "Feb" => "02", "Mar" => "03", "Apr" => "04", "May" => "05", "Jun" => "06",
  21.                     "Jul" => "07", "Aug" => "08", "Sep" => "09", "Oct" => "10", "Nov" => "11", "Dec" => "12" );
  22.     for (keys %mnths) { if ($d&#91;1] eq $_) { $d[1] = $mnths{$_}; last } }
  23.     $d&#91;3] =~ s/://g;
  24.     return "$d[2]$d[1]$d[0]$d[3]";
  25. }

Run this script with two arguments.
scriptname "<start_time>" "<end_time>"
For e.g. this will be the output for input provided in post #1

Code:
$ ./test.pl "25 Dec 2011 03:30:59" "27 Dec 2011 12:45:00"
25 Dec 2011 04:23:23 : bbbbbbbbbbbbbbb
25 Dec 2011 16:12:45 : ccccccccccccccc
26 Dec 2011 04:45:34 : ddddddddddddddd
26 Dec 2011 17:01:22 : eeeeeeeeeeeeeee
27 Dec 2011 12:33:45 : ffffffffffffffffffffffff


Last edited by balajesuri; 12-29-2011 at 07:58 AM..
# 5  
Old 12-29-2011
Thanks balajesuri,
but is there any other way except using perl..

Using any command in unix whether awk/sed,etc????

---------- Post updated at 11:47 PM ---------- Previous update was at 10:20 PM ----------

Guys... i need help on this..!!!
# 6  
Old 12-29-2011
bash code:
  1. #! /bin/bash
  2. &#91; $# -ne 2 ] && echo "Invalid Parameters. Exiting." && exit
  3.  
  4. st_date=`date -d "$1" +%Y%m%d%H%M%S`
  5. nd_date=`date -d "$2" +%Y%m%d%H%M%S`
  6.  
  7. while read x
  8. do
  9.     dt=$(date -d "`echo $x | cut -c1-20`" +%Y%m%d%H%M%S)
  10.     &#91; $dt -ge $st_date -a $dt -le $nd_date ] && echo $x
  11. done < inputfile.txt

Code:
$ ./test.sh "25 Dec 2011 03:30:30" "27 Dec 2011 12:45:00"
25 Dec 2011 04:23:23 : bbbbbbbbbbbbbbb
25 Dec 2011 16:12:45 : ccccccccccccccc
26 Dec 2011 04:45:34 : ddddddddddddddd
26 Dec 2011 17:01:22 : eeeeeeeeeeeeeee
27 Dec 2011 12:33:45 : ffffffffffffffffffffffff

# 7  
Old 12-29-2011
What the Invalid Paramter existing???

Please explain the script if you can.
Thanks..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Fetch data from file

Hi, I am new to scripting. I have a log file and need to fetch specific logs and copy to another file. A copy of the log is like this: =============================================================== = JOB : server123#jobs1.jobstream1 = USER : andyc = Tue 08/01/17... (3 Replies)
Discussion started by: Prngp
3 Replies

2. UNIX for Beginners Questions & Answers

How to fetch specific data from a file.?

Hi , I have a file which contains 2 days logs(here it is 24 and 25) I want to list data only for date 25 fron the file. please suggest me how should i get this. file content mentioned below 17-05-24 Name Succ Fail 00:00:29 ... (5 Replies)
Discussion started by: scriptor
5 Replies

3. Shell Programming and Scripting

Need to fetch only selected data in CSV

Hi Team, I m getting my script commands output like given below GETA-TILL-INF; U-UU-YRYT-NOD-6002 2015-05-14 THU 19:44:10 C2221 RETRIEVE TILL INFORMATION : COMPLD ---------------------------------------------------------------------- CONNECT_CARD_ID ... (9 Replies)
Discussion started by: Ganesh Mankar
9 Replies

4. Shell Programming and Scripting

Help Need to fetch the required data

Hi Guys, Am in need of your help one more time on my real data. I have a file which contains more than thousand lines of data Live data shown for 4 iterations. We have more than thousand lines of data:- -------------------------------------------------------------------------- ... (4 Replies)
Discussion started by: rocky2013
4 Replies

5. UNIX for Dummies Questions & Answers

how to fetch data in unix

Hi All, I have a file with the below data as shown. A|2|20120430 B|EMP|NAME|DEPT C|12|SARC|01 C|23||ASDD|02 D|END OF FILE I want to fetch only the records that contains C|, what is unix command to fetch this data. Thanks (5 Replies)
Discussion started by: halpavan2
5 Replies

6. Shell Programming and Scripting

Fetch data between two dates from a file

Hi All, I m new to this forum & UNix too. currently i have a requirement which can fetch data from a logfile between two dates or timestamp. for example: 1. data from 2012 Jun to 2012 Jul 2. data from 2012 Jun to 2012 Jul 07 3. data from 2012 Jun 16 10:20 to 2012 Jul 03 10:10 Please... (7 Replies)
Discussion started by: KDMishra
7 Replies

7. Shell Programming and Scripting

Fetch data from a particular location

I want to fetch value from a particular location from a file but in each line in the file it appears at a different position so i tried using variable with cut command but it is not working properly. The code i have written is #!/bin/sh cat Sri1.log | while read d2 do grep -w... (9 Replies)
Discussion started by: Prachi Gupta
9 Replies

8. Shell Programming and Scripting

Script to fetch data from HTML

Hi All, There is a link from were I usually search somthing and fetch the data from. Is there any way to automate it through a script if I mention search criteria in a note pad. I mean the script to search the content on the notepad and resutls should be placed into another file. ... (2 Replies)
Discussion started by: indradev
2 Replies

9. Shell Programming and Scripting

How to fetch data from oracle in unix shell script

Hi, How to fetch data from oracle database in unix shell scripting. list=`sqlplus -s ds_user/dsuser@EMI <<EOF set feedback off set serveroutput on set heading off set pagesize 0 set tab off select IP_ID from table / exit EOF` The output is not what i expected.I need output in... (4 Replies)
Discussion started by: Anusha_Reddy
4 Replies

10. Shell Programming and Scripting

Fetch selected data from webpage

Hi All, Can anybody tell me the command used for extracting some selected lines from a web-page. I guess we'll have to do this using wget or Curl to achieve this.... If anbody has any idea abt it, kindly post your reply ASAP. Thanks. (1 Reply)
Discussion started by: sunnydynamic15
1 Replies
Login or Register to Ask a Question