To search lines between two timestamps in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting To search lines between two timestamps in a file
# 1  
Old 08-28-2012
To search lines between two timestamps in a file

I have a log file where every line starts with a time stamp. I have to extract lines from the file within a given time stamp.
For example:
IF the file is like this:
Code:
2012-08-19 10:34:03,446|WebContainer : 56|OrderHeaderDaoImpl|findByKeys|26|
2012-08-20 11:34:03,463|WebContainer : 56|OrderHeaderDaoImpl|addData|17|
2012-08-21 12:34:03,694|WebContainer : 56|LineItemRules|lineItemRules|231|
2012-08-22 13:34:03,704|WebContainer : 56|ShipmentServiceImpl|updateShipmentsAndNotes|10|
2012-08-22 16:34:03,842|WebContainer : 56|XFILI212ClientImpl|calculateTax|138|
2012-08-23 10:34:03,842|WebContainer :

and I need the data between the time stamps 2012-08-20 11:30 and 2012-08-22 16:00
then I should be able to extract the data lines:
Code:
2012-08-20 11:34:03,463|WebContainer : 56|OrderHeaderDaoImpl|addData|17|
2012-08-21 12:34:03,694|WebContainer : 56|LineItemRules|lineItemRules|231|
2012-08-22 13:34:03,704|WebContainer : 56|ShipmentServiceImpl|updateShipmentsAndNotes|10|

I have tried using awk -v but if the time stamp is not found in the file it is giving some false output. Please help.

Thanks in Advance.

Last edited by Franklin52; 08-28-2012 at 08:29 AM.. Reason: Please use code tags for data and code samples
# 2  
Old 08-28-2012
If you know the exact timestamp, you can use this:

Code:
sed -n '/2012-08-20 11:34/,/2012-08-22 16:34/p' file


Last edited by Subbeh; 08-28-2012 at 09:10 AM.. Reason: I didn't see that you already got that far
# 3  
Old 08-28-2012
with awk :
Code:
b="2012-08-20 11:34:03"
e="2012-08-22 13:34:03"
awk -v "b=$b" -v "e=$e" -F ',' '$1 == b,$1 == e' file

(edit) correction :
Code:
b="2012-08-20 11:34:03"
e="2012-08-22 13:34:03"
awk -v "b=$b" -v "e=$e" -F ',' '$1 >= b && $1 <= e' file


Last edited by delugeag; 08-28-2012 at 09:52 AM..
# 4  
Old 08-28-2012
With gawk:
Code:
gawk -F, -v start='2012-08-20 11:30:00' -v end='2012-08-22 16:00:00' '
BEGIN{
OFS=FS
gsub(/[-:]/," ",start);gsub(/[-:]/," ",end)
start=mktime(start);end=mktime(end)
}
{
 t=$1
 gsub(/[-:]/," ",t)
 linetime=mktime(t)
 if(linetime>=start && linetime<=end)
  print
}' infile

# 5  
Old 08-28-2012
This works as well:

Code:
cat file | awk -F, '{ if ($1>"2012-08-20 11:30" && $1<"2012-08-22 16:00") print }'

It doesn't work when I store the values in variables though.

Last edited by Subbeh; 08-28-2012 at 09:48 AM.. Reason: Wrong code
# 6  
Old 08-28-2012
Quote:
Originally Posted by Subbeh
This works as well:

Code:
cat file | awk -F, '{ if ($1>"2012-08-20 11:30" && $1<"2012-08-22 16:00") print }'

It doesn't work when I store the values in variables though.
Try with variable..

Code:
 awk -F, -v STD="2012-08-20 11:30" -v ED="2012-08-22 16:00" '{ if ($1 > STD && $1 < ED) print }' file

# 7  
Old 08-28-2012
Quote:
Originally Posted by pamu
Try with variable..

Code:
 awk -F, -v STD="2012-08-20 11:30" -v ED="2012-08-22 16:00" '{ if ($1 > STD && $1 < ED) print }' file

That doesn't work for me:

Code:
awk -F, -v STD="2012-08-20 11:30" -v ED="2012-08-22 16:00" '{ if ($1 > STD && $1 < ED) print }' testfile
awk: syntax error near line 1
awk: bailing out near line 1

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search lines between two timestamps in a file

Hi, I want to pull lines between two timestamps from a file. When I just insert values directly it is working fine. sed -n '/2014-02-14 05:30/,/2014-02-14 05:31/p' Logfile When I try to insert variable it is not working. sed -n '/TZ=GMT+1 date +%Y-%m-%d" "%H:%M:%S/,/TZ=GMT date... (2 Replies)
Discussion started by: Neethu
2 Replies

2. Shell Programming and Scripting

To print lines between 2 timestamps using awk|sed and regex

Hi, I am using the following code to fetch lines that are generated in last 1 hr . Hence, I am using date function to calculate -last 1 hr & the current hr and then somehow use awk (or sed-if someone could guide me better) with some regex pattern. dt_1=`date +%h" "%d", "%Y\ %l -d "1 hour... (10 Replies)
Discussion started by: sarah-alikhan31
10 Replies

3. Shell Programming and Scripting

How to Search string(which is in 2 lines) in a file?

Hello, I want to search 2 lines from the file. I wanted to display all such matches. Example file: ================== Testfile is test TEST1 TEST2 testing the file string to do testing TEST1 TEST2 sample strings ================= I wanted to search the file with 2 lines " TEST1... (3 Replies)
Discussion started by: balareddy
3 Replies

4. Shell Programming and Scripting

search and delete the lines in a file

HI group members I am new in unix I want to search # symbol in a file. if found need to delete the entire row in the file. need to move the actual data(with out # symbol data) to another file. Thanks (2 Replies)
Discussion started by: pmreddy
2 Replies

5. Shell Programming and Scripting

awk how to search strings within a file from two different lines

Hi, i would really appreciate any help anyone can give with the following info. Thanks in advance. I need to run a search on a file that contains thousands of trades, each trade is added into the file in blocks of 25 lines. i know the search has to take place between a time stamp specified... (4 Replies)
Discussion started by: sp3arsy
4 Replies

6. UNIX for Dummies Questions & Answers

Search for lines in one file in another

I am trying to search for lines present in file a that do not exist in file b and print out the lines. E.g. file a apple pear banana orange file b apple banana orange would output to stdout: pear. ... (3 Replies)
Discussion started by: thurmc
3 Replies

7. Shell Programming and Scripting

search for an expression in a file and print the 3 lines above it

Hi, I have a file like this comment.txt 1.img 2.img 3.img OK x.img y.img z.img not ok 1.img 2.img 3.img bad 1.img 2.img 3.img (7 Replies)
Discussion started by: avatar_007
7 Replies

8. Shell Programming and Scripting

Search and Remove Lines within File

Hello, I've searched through the scripting section but could not find what I need. I need to search for empty sections within a file and remove them. Here is an example file: Title 123 A B C D E Title 098 Title 567 Z Y (4 Replies)
Discussion started by: leepet01
4 Replies

9. Shell Programming and Scripting

search for lines in a file

Hello I need to check if following three files exist in a file, how to do that in shell script: 1. ALL MACHING RECORD COLUMNS MATCHED (Baseline and Regression File) 2. Total Mismatched Records (Baseline File): 0 3. Total Mismatched Records (Regression File): 0 Currently I am seaching only... (9 Replies)
Discussion started by: shalua
9 Replies

10. Shell Programming and Scripting

shell script to search content of file with timestamps in the directory

hello, i want to make a script to search the file contents in my home directory by a given date and output me the line that has the date... (10 Replies)
Discussion started by: psychobeauty
10 Replies
Login or Register to Ask a Question