Grep content between timestamp


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grep content between timestamp
# 15  
Old 10-01-2010
Thanks kesavan .. Its working fine..

But one small bug is thr.. its taking the current timing content..

Ex: If now system time is 14:10. script should grep content between 13:30 to 14:00

but your script is taking 14:00 content.. pls modify as per this need and telme .

Thanks a Lot...

Steve

---------- Post updated at 06:44 PM ---------- Previous update was at 06:29 PM ----------

HI kesavan ,

Last line of your script is not working .. pls check...

Code:
LGS-HF2-2$ sed -i '1,2d' last_half_hour.txt 
sed: illegal option -- i

# 16  
Old 10-01-2010
MySQL Grep content between timestamp

Quote:
Originally Posted by steve2216
Thanks kesavan .. Its working fine..

But one small bug is thr.. its taking the current timing content..

Ex: If now system time is 14:10. script should grep content between 13:30 to 14:00

but your script is taking 14:00 content.. pls modify as per this need and telme .

Thanks a Lot...

Steve

---------- Post updated at 06:44 PM ---------- Previous update was at 06:29 PM ----------

HI kesavan ,

Last line of your script is not working .. pls check...

Code:
LGS-HF2-2$ sed -i '1,2d' last_half_hour.txt 
sed: illegal option -- i

-i option is for in place edition without redirection
but i don't get why it is not working for you...
anyway try this one...

Code:
#!/bin/sh
sed -n '/[0-9][0-9]\/[0-9][0-9]\/[0-9][0-9][0-9][0-9]/p' time.txt | sed  -e 's/ /./' -e 's/\//./g' > time_stamps.txt
last_time_stamp=`sed -n '$'p time_stamps.txt`
sed  '$'d time_stamps.txt > temp0.txt
mv temp0.txt time_stamps.txt
last_time_stamp1=`sed -n '$'p time_stamps.txt`
sed -n '/'$last_time_stamp1'/,/'$last_time_stamp'/'p time.txt > last_half_hour.txt
sed  '1'd last_half_hour.txt > temp.txt
sed  '$'d temp.txt > temp1.txt
mv  temp1.txt last_half_hour.txt

Now check the "last_half_hour.txt" output file Smilie....

Last edited by Franklin52; 10-01-2010 at 12:57 PM.. Reason: adding code tags
This User Gave Thanks to Kesavan For This Post:
# 17  
Old 10-01-2010
Thanks Kesavan !!!!!

Its working fine now.. Smilie

Tanks a lot.. Smilie

Steve
# 18  
Old 10-01-2010
Bug

Quote:
Originally Posted by steve2216
Thanks Kesavan !!!!!

Its working fine now.. Smilie

Tanks a lot.. Smilie

Steve
You are welcome...
# 19  
Old 10-15-2010
Hi Kesavan,

Can you pls explain your below script? thanks

Code:
#!/bin/sh
sed -n '/[0-9][0-9]\/[0-9][0-9]\/[0-9][0-9][0-9][0-9]/p' time.txt | sed  -e 's/ /./' -e 's/\//./g' > time_stamps.txt
last_time_stamp=`sed -n '$'p time_stamps.txt`
sed  '$'d time_stamps.txt > temp0.txt
mv temp0.txt time_stamps.txt
last_time_stamp1=`sed -n '$'p time_stamps.txt`
sed -n '/'$last_time_stamp1'/,/'$last_time_stamp'/'p time.txt > last_half_hour.txt
sed  '1'd last_half_hour.txt > temp.txt
sed  '$'d temp.txt > temp1.txt
mv  temp1.txt last_half_hour.txt

# 20  
Old 10-18-2010
Code:
my ($pre,$cur);
while(<DATA>){
  if (/([0-9]{2})\/([0-9]{2})\/([0-9]{4})\s+([0-9]{2}):([0-9]{2})/){
    $temp = $3."_".$2."_".$1."_".$4."_".$5;
    if($.==1){
       $cur = $temp;
       $pre = $temp;
    }
    else{
      $cur = $temp;
      my $tmp = ${pre}."-".$cur.".leo";
      open FH,">$tmp";
      print FH @{$hash{$pre}};
      close FH;
      @{$hash{$temp}}=(@{$hash{$pre}});
      $pre = $temp;
    }
    next;
  }
  else{
    push @{$hash{$temp}},$_;
  }
}
my $temp=${pre}."_now.leo";
open FH,">$temp";
print FH @{$hash{$pre}};
close FH;
__DATA__
29/09/2010 00:30
line 1
line 2
29/09/2010 01:00
line 3
line 4
29/09/2010 01:30
line 5
line 6
29/09/2010 02:30
line 7
line 8

# 21  
Old 10-20-2010
MySQL Grep content between timestamp

Quote:
Originally Posted by steve2216
Hi Kesavan,

Can you pls explain your below script? thanks

Code:
#!/bin/sh
sed -n '/[0-9][0-9]\/[0-9][0-9]\/[0-9][0-9][0-9][0-9]/p' time.txt | sed  -e 's/ /./' -e 's/\//./g' > time_stamps.txt

#This will grep all the time stamps and puts it into a file
Code:
last_time_stamp=`sed -n '$'p time_stamps.txt`

#This will get the last time stamp
Code:
sed  '$'d time_stamps.txt > temp0.txt

#This deletes the last time stamp
Code:
mv temp0.txt time_stamps.txt

#Now the half an hour before time stamp will be last
Code:
last_time_stamp1=`sed -n '$'p time_stamps.txt`

#This gets the half an hour before time stamp
Code:
sed -n '/'$last_time_stamp1'/,/'$last_time_stamp'/'p time.txt > last_half_hour.txt

#This prints the content between the time stamps including the time stamps also
Code:
sed  '1'd last_half_hour.txt > temp.txt

#deleting half an hour before time stamp
Code:
sed  '$'d temp.txt > temp1.txt

# deleting last time stamp
Code:
mv  temp1.txt last_half_hour.txt

Comment the lines and execute the script for better understanding and see the files what is happening in them
Experiment
Smilie

Last edited by Kesavan; 10-20-2010 at 05:17 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep lines between last hour timestamp and current timestamp

So basically I have a log file and each line in this log file starts with a timestamp: MON DD HH:MM:SS SEP 15 07:30:01 I need to grep all the lines between last hour timestamp and current timestamp. Then these lines will be moved to a tmp file from which I will grep for particular strings. ... (1 Reply)
Discussion started by: nms
1 Replies

2. Shell Programming and Scripting

Append timestamp create .trg file for all content of an unzipped archive

Hi, I have a test.zip archive that contains test.zip --> (file_1.txt, file_2.txt , file_3.txt) I need to unzip the file like this, file_1_timestamp.txt file_1_timestamp.trg file_2_timestamp.txt file_2_timestamp.trg file_3_timestamp.txt file_3_timestamp.trg Could you please let me know... (7 Replies)
Discussion started by: Shandel
7 Replies

3. UNIX for Dummies Questions & Answers

Grep content in xml file

I have an xml file with header as below. <Provider xmlns="http://www.xyzx.gov/xyz" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.xyzx.gov/xyz xyz.xsd" SCHEMA_VERSION="2.5" PROVIDER="5"> I want to get the schema version here that is 2.5 and put in a... (7 Replies)
Discussion started by: Ariean
7 Replies

4. Shell Programming and Scripting

How to sort grep result based on timestamp?

Hi, Trying to sort grep result based on timestamp of the filename. I have the following result and want to sort them on timestampgrep -i 'ERROR' *log*2013* s_m_xxx_xxx_xxx_xxx_xxxx.log.20130906092431:TRANSF_1_1_1> DBG_21216 Finished transformations for Source Qualifier . Total errors ... (5 Replies)
Discussion started by: bobbygsk
5 Replies

5. Shell Programming and Scripting

How to grep the content performed by an User

I want to grep the content performed by an User from a file. Suppose that i have a following file HYD-HMS-2$ ls -lrt -rw-r--r-- 1 sdfrun sdf 31726356 Aug 1 13:04 journal.03.01082012.19.csv I could able to grep the content performed by a user by "sed" command as follows HYD-HMS-2$... (0 Replies)
Discussion started by: duppalav
0 Replies

6. Shell Programming and Scripting

how to get tags content by grep

1) Is it possible to get tags content by grep -E ? For example title. Source text "<title>My page<title>"; to print "My page". 2) which bash utility to use when I want to use regex in this format? (?<=title>).*(?=</title) (11 Replies)
Discussion started by: visitor123
11 Replies

7. UNIX for Dummies Questions & Answers

Append file with grep output but add timestamp?

I've setup a cron job that greps a file every five minutes and then writes (appends) the grep output/result to another file: grep "monkey" zoo.log | tail -1 >> cron-zoo-log Is there any way I can add the date and time (timestamp) to the cron-zoo-log file for each time a new line was added? ... (12 Replies)
Discussion started by: Sepia
12 Replies

8. AIX

how to grep and compare timestamp in a file with the current date

I want to read a log file from a particular location.In the log file each line starts with timestamp.I need to compare the timestamp in the logfile with the current date.If the timpestamp in the log file is less than 4 hours then i need to read the file from that location.Below is the file... (1 Reply)
Discussion started by: achu
1 Replies

9. AIX

how to grep and compare timestamp in a file with the current date

I want to read a log file from a particular location.In the logfile , lines contains timestamp.I need to compare the timestamp in the logfile with the current date.If the timpestamp in the log file is less than 4 hours then i need to read the file from that location.Below is the file format.Please... (1 Reply)
Discussion started by: achu
1 Replies

10. Shell Programming and Scripting

how to grep the logs for two particular timestamp

Hi, could anyone help me out how to write a script, to grep the two timestamp from a particular file, so that it will list out all the logs between the particular timestamp I have a pattern of log: servicename operationname starttime endtime eg., servicename1 operationname1 01:11:11... (1 Reply)
Discussion started by: jacktolearn
1 Replies
Login or Register to Ask a Question