Extracting lines in file based on time


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extracting lines in file based on time
# 1  
Old 07-14-2009
Extracting lines in file based on time

Hi,

anyone has any ideas on how do we extract lines from a file with format similiar to this: (based on current time)

Jun 18 00:16:50 .......... ............. ............
Jun 18 00:17:59 .......... ............. ............
Jun 18 01:17:20 .......... ............. ............
Jun 18 01:18:51 .......... ............. ............

for example: the time now is 01:20:00 and the result will return me records no later than 15mins ago.
Jun 18 01:17:20 .......... ............. ............
Jun 18 01:18:51 .......... ............. ............

by the way, i'm using shell script
# 2  
Old 07-14-2009
If you can share the script that you have done, we can extend it for additional requirement...
# 3  
Old 07-14-2009
Currently what i've done is only to retrieve records no later than an hour.
want to extend the feature to include retrieving of records within 15mins of current time. I don't think my current method can be applicable to 15mins intervals..

Code:
 
str=`TZ=GMT-7 date +%b" "%d" "%H:` # this sets a time earlier by one hour.
 
#echo str will give you something like "Jun 18 01:"
 

grep "$str" test.log > new.log  # test.log refers to the file format that i've mentioned earlier.

# 4  
Old 07-15-2009
Anyone has any idea on how to go about doing it?
# 5  
Old 07-17-2009
any help?
# 6  
Old 07-17-2009
Assuming Jun 18 does not change, and only time stame HH:MI:SS is considered
Try:
Code:
#!/bin/ksh
#set -x

_minPrev=15
_currTimeInSec=$(echo `date "+%T"` | awk -F":" '{print $1*3600+$2*60+$3}')
(( _prevTimeInSec = ${_currTimeInSec} - (0+60*${_minPrev}+0) ))

while read line
do
   _timeInFile=$(echo $line | awk '{FS="[: ]+"}{print $3*3600+$4*60+$5}')

   if [[ ${_timeInFile} -ge ${_prevTimeInSec} && ${_timeInFile} -le ${_currTimeInSec} ]]; then
      echo $line
   fi
done < input


I think extending it for MM and DD should not be an issue for you.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extracting words and lines based on keywords

Hello! I'm trying to process a text file and am stuck at 2 extractions. Hoping someone can help me here: 1. Given a line in a text file and given a keyword, how can I extract the word preceeding the keyword using a shell command/script? For example: Given a keyword "world" in the line: ... (2 Replies)
Discussion started by: seemad
2 Replies

2. Shell Programming and Scripting

Extracting lines from text files in folder based on the numbers in another file

Hello, I have a file ff.txt that looks as follows *ABNA.txt 356 24 36 112 *AC24.txt 457 458 321 2 ABNA.txt and AC24.txt are the files in the folder named foo1. Based on the numbers in the ff.txt file, I want to extract the lines from the corresponding files in the foo1 folder and... (2 Replies)
Discussion started by: mohamad
2 Replies

3. Shell Programming and Scripting

Extracting log files based on date and time.

Hi All, i have some log files generated in a folder daily with the format abc.def.20130306.100001 ghi.jkl.20130306.100203 abc.def.20130305.100001 ghi.jkl.20130305.100203 the format is the date followed by time . all i want is to get the files that are generated for todays... (3 Replies)
Discussion started by: mahesh300182
3 Replies

4. UNIX for Dummies Questions & Answers

Extracting lines from a text file based on another text file with line numbers

Hi, I am trying to extract lines from a text file given a text file containing line numbers to be extracted from the first file. How do I go about doing this? Thanks! (1 Reply)
Discussion started by: evelibertine
1 Replies

5. Shell Programming and Scripting

extracting lines based on condition and copy to another file

hi i have an input file that contains some thing like this aaa acc aa abc1 1232 aaa abc2.... poo awq aa abc1 aaa aaa abc2 bbb bcc bb abc1 3214 bbb abc3.... bab bbc bz abc1 3214 bbb abc3.... vvv ssa as abc1 o09 aaa abc4.... azx aaq aa abc1 900 aqq abc19.... aaa aa aaaa abc1 899 aa... (8 Replies)
Discussion started by: anurupa777
8 Replies

6. Shell Programming and Scripting

Extracting lines based on identifiers into multiple files respectively

consider the following is the contents of the file cat 11.sql drop procedure if exists hoop1 ; Delimiter $$ CREATE PROCEDURE hoop1(id int) BEGIN END $$ Delimiter ; . . . . drop procedure if exists hoop2; Delimiter $$ CREATE PROCEDURE hoop2(id int) BEGIN END $$ (8 Replies)
Discussion started by: vivek d r
8 Replies

7. Shell Programming and Scripting

Extracting few lines from a file based on identifiers dynamically

i have something like this in a file called mysqldump.sql -- -- Table structure for table `Table11` -- DROP TABLE IF EXISTS `Table11`; /*!40101 SET @saved_cs_client = @@character_set_client */; /*!40101 SET character_set_client = utf8 */; CREATE TABLE `Table11` ( `id` int(11) NOT NULL... (14 Replies)
Discussion started by: vivek d r
14 Replies

8. 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

9. Shell Programming and Scripting

awk : extracting unique lines based on columns

Hi, snp.txt CHR_A SNP_A BP_A_st BP_A_End CHR_B BP_B SNP_B R2 p-SNP_A p-SNP_B 5 rs1988728 74904317 74904318 5 74960646 rs1427924 0.377333 0.000740085 0.013930081 5 ... (12 Replies)
Discussion started by: genehunter
12 Replies

10. Shell Programming and Scripting

Remove lines, Sorted with Time based columns using AWK & SORT

Hi having a file as follows MediaErr.log 84 Server1 Policy1 Schedule1 master1 05/08/2008 02:12:16 84 Server1 Policy1 Schedule1 master1 05/08/2008 02:22:47 84 Server1 Policy1 Schedule1 master1 05/08/2008 03:41:26 84 Server1 Policy1 ... (1 Reply)
Discussion started by: karthikn7974
1 Replies
Login or Register to Ask a Question