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.
$# --> 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.
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..
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.
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.
$ ./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..!
---------- Post updated at 01:02 AM ---------- Previous update was at 12:54 AM ----------
--> 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.
$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