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
# 8  
Old 12-29-2011
Line 2: When you run the script from command line, you've to provide exactly 2 parameters. Otherwise, it'll exit.
Line 4,5: The start and end timestamp that you enter as 1st and 2nd parameters while invoking the script are converted to YYYYMMDDHHMMSS format (a single 14 digit number).
Line 7: Input file is read line by line in a while loop
Line 9: The timestamp at the beginning of each line (first 20 characters) in input file is converted to YYYYMMDDHHMMSS format.
Line 10: Date obtained in line 9 is checked with start time stamp and end time stamp found in line 4 & 5. If >= start time and <= end time, then print the line.
# 9  
Old 12-29-2011
Code:
[ $# -ne 2 ] && echo "Invalid Parameters. Exiting." && exit

what does the above line mean???
and why the invalid parameters??

Last edited by Franklin52; 12-30-2011 at 08:56 AM.. Reason: Please use code tags for data and code samples, thank you
# 10  
Old 12-29-2011
$# --> Refers to the number of parameters provided at command line alongside scriptname.
[ $# -ne 2 ] --> Number of parameters ($#) is not equal (-ne) to two (2)
&& --> Logical 'and' operator.
"Invalid Parameters. Exiting" --> This will be printed on screen if you don't provide exactly two parameters. Ok, my mistake.. I should've mentioned "Invalid number of parameters. Exiting."
&& --> Another logical 'and' operator
exit --> Script will exit the moment this keyword in encountered.

So, summing it up, if [ $# -ne 2 ] is true, then do echo "Invalid Parameters. Exiting.". And if that is true then do exit

This whole line is just a check to ensure that user is entering exactly two parameters alongside script name while invoking it at command line. This line is not necessary, if the user knows what exactly to enter and how much to enter.
This User Gave Thanks to balajesuri For This Post:
# 11  
Old 12-29-2011
Code:
JAI@JAI-PC ~/hello/Recycle/check/pearl
$ ./test1.sh "26 Dec 2011 11:39:43,070" "28 Dec 2011 09:15:17"
./test1.sh: line 1: $'\r': command not found
./test1.sh: line 4: $'\r': command not found
./test1.sh: line 7: $'\r': command not found
./test1.sh: line 12: syntax error near unexpected token `done'
./test1.sh: line 12: `done < New_File.txt'

I am getting the below errors while running this script..

Last edited by Franklin52; 12-30-2011 at 08:57 AM.. Reason: Code tags
# 12  
Old 12-29-2011
In the first parameter why did you give milliseconds? -- 26 Dec 2011 11:39:43,070
That was not your original requirement right? Anyway, that shouldn't matter.

Post the output of the following command:
Code:
cat -A test1.sh


This User Gave Thanks to balajesuri For This Post:
# 13  
Old 12-29-2011
I recently tried the below command.
Code:
sed -n '/28 Dec 2011 10:17:53/,/28 Dec 2011 18:43:07/p' filename

But it shows some problem..

---------- Post updated at 12:45 AM ---------- Previous update was at 12:42 AM ----------

THats correct.

But still it is throwing me an error.


Code:
$ ./test1.sh "26 Dec 2011 11:39:43" "28 Dec 2011 09:15:17"
./test1.sh: line 1: $'\r': command not found
./test1.sh: line 4: $'\r': command not found
./test1.sh: line 7: $'\r': command not found
./test1.sh: line 12: syntax error near unexpected token `done'
./test1.sh: line 12: `done < New_BPPError.log'


I need to fetch data between two dates with timestamp.
It may occur that the starting date is not present in the file with the same timestamp.
The timestamp can vary. I need to apply the between condition.

Please help.!!

---------- Post updated at 12:54 AM ---------- Previous update was at 12:45 AM ----------

Hey..

Its working fine..
Thanks a ton..
You have been great help Mr.balajesuri..! Smilie

---------- Post updated at 01:02 AM ---------- Previous update was at 12:54 AM ----------

Can you explain me the below part of the script.


Code:
    dt=$(date -d "`echo $x | cut -c1-20`" +%Y%m%d%H%M%S)
    [ $dt -ge $st_date -a $dt -le $nd_date ] && echo $x


Last edited by Franklin52; 12-30-2011 at 08:58 AM.. Reason: Code tags
# 14  
Old 12-29-2011
Code:
dt=$(date -d "`echo $x | cut -c1-20`" +%Y%m%d%H%M%S)

--> Break this into pieces. echo $x | cut -c1-20 fetches the first 20 characters of each line of log file. So, this refers to the time stamp.
--> -d switch of date command shows date as specified by the following string. Think of this as an intelligent switch of date command. For e.g., if you say date -d "1 day ago", it would display the previous days' date.
--> +%Y%m%d%H%M%S is the format in which date should be printed. Check man pages of date command.
So date -d "25 Dec 2011 10:10:10" +%Y%m%d%H%M%S would show 20111225101010. Check it out.

Code:
[ $dt -ge $st_date -a $dt -le $nd_date ] && echo $x

$dt -ge $st_date --> if value in variable dt ($dt) is greater than equal to (-ge) value in st_date ($st_date)
-a --> logical 'and' operator
$dt -le $nd_date --> if value in variable dt ($dt) is less than equal to (-le) value in nd_date ($nd_date)
&& --> another logical 'and' operator
echo $x --> print line ($x) on screen
So summing it up, if [ $dt -ge $st_date -a $dt -le $nd_date ] is true then do echo $x
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