select the lines in between some time span


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting select the lines in between some time span
# 1  
Old 10-20-2011
select the lines in between some time span

Hi Everyone !


i want to take all the lines from a file that falls in between some date... and every line in a file has a time stamp..

Code:
---some text---- 01/Jan/2010 ---- some other text ----
---some text---- 10/Jan/2010 ---- some other text ----
---some text---- 20/Dec/2010 ---- some other text ----
---some text---- 01/Jan/2011 ---- some other text ----
---some text---- 02/Jan/2011 ---- some other text ----

suppose i want to select the lines that satisfies date between 09/Jan/2010 and 01/Jan/2011

result:
Code:
---some text---- 10/Jan/2010 ---- some other text ----
---some text---- 20/Dec/2010 ---- some other text ----
---some text---- 01/Jan/2011 ---- some other text ----

how to collect those lines from a log file?

so far i'm able to do this
Code:
current_date = date +%d/%b/%Y
Given_date = date -d "-3000 day" +%/d/%b/%Y

i'll get some time span
Code:
upper limit = 20/Oct/2011
Lower limit = 25/May/2001

i want to select the lines in which lines falls in between 20/Oct/2011 and 25/May/2001

Regards,

Ewa

Moderator's Comments:
Mod Comment Please use code tags


---------- Post updated at 08:10 AM ---------- Previous update was at 07:36 AM ----------

a function or line of code which i can put in while loop

while read line
do
result = if satisfy the condition
result>>newfile

done<logfile

Last edited by zaxxon; 10-20-2011 at 10:09 AM.. Reason: code tags
# 2  
Old 10-20-2011
Code:
#!/usr/bin/ksh
typeset -i mI
set -A mMon n/a Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
mFrom='20100105'
mTo='20110101'
while read mLine; do
  echo ${mLine} | sed 's#.*\(..\)/\(...\)/\(....\).*#\1 \2 \3#' | read mDD mMMM mYYYY
  mI=1
  while [[ ${mI} -le 12 ]]; do
    if [[ "${mMon[$mI]}" = "${mMMM}" ]]; then
      break
    fi
    mI=${mI}+1
  done
  if [[ ${mI} -lt 10 ]]; then
    mYYYYMMDD=${mYYYY}'0'${mI}${mDD}
  else
    mYYYYMMDD=${mYYYY}${mI}${mDD}
  fi
  if [[ ${mYYYYMMDD} -ge ${mFrom} && ${mYYYYMMDD} -le ${mTo} ]]; then
    echo ${mLine}
  fi
done < File

This User Gave Thanks to Shell_Life For This Post:
# 3  
Old 10-20-2011
Hi

sorry i didn't mention in the beginning that i'm using BOURNE SHELL (bash)? it wouldn't work i guess

BR

Ewa
# 4  
Old 10-20-2011
Hi me_newbie,

A possible solution using 'perl':
Code:
$ cat infile 
---some text---- 01/Jan/2010 ---- some other text ----
---some text---- 10/Jan/2010 ---- some other text ----
---some text---- 20/Dec/2010 ---- some other text ----
---some text---- 01/Jan/2011 ---- some other text ----
---some text---- 02/Jan/2011 ---- some other text ----
$ cat script.pl
use warnings;
use strict;
use POSIX;

my %TAB_MONTH = ( 
        jan     =>      0,
        feb     =>      1,
        mar     =>      2,
        apr     =>      3,
        may     =>      4,
        jun     =>      5,
        jul     =>      6,
        aug     =>      7,
        sep     =>      8,
        oct     =>      9,
        nov     =>      10,
        dec     =>      11
);

die qq[Usage: perl $0 input-file date-from date-to\n] unless @ARGV == 3;

my $utc_date_to = date_str_to_utc( pop @ARGV ) or die qq[Bad format of date (dd/mmm/yyyy)\n];
my $utc_date_from = date_str_to_utc( pop @ARGV ) or die qq[Bad format of date (dd/mmm/yyyy)\n];

while ( <> ) {
        next if m/\A\s*\z/;
        chomp;
        my $utc_date_line = date_str_to_utc( $_ ) or next; 
        if ( $utc_date_line >= $utc_date_from && $utc_date_line <= $utc_date_to ) {
                printf "%s\n", $_;
        }
}


sub date_str_to_utc {
        my ($date_str) = $_[0];

        my ($mday,$month,$year) = $date_str =~ m|\b(\d{2})/(\w{3})/(\d{4})| or return;
        my $utc_date = POSIX::mktime( 0, 0, 0, $mday, $TAB_MONTH{ lc $month }, $year - 1900 );

        return $utc_date;
}
$ perl script.pl infile "09/Jan/2010" "01/Jan/2011"
---some text---- 10/Jan/2010 ---- some other text ----
---some text---- 20/Dec/2010 ---- some other text ----
---some text---- 01/Jan/2011 ---- some other text ----

Regards,
Birei
This User Gave Thanks to birei For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Total size utilizes by the files older than a time span

Through find command I identified the files older that 1 year. I need the overall size utilizes by these 1 year older files. Please share me the command to identify it .Thanks Please post in an adequate technical forum! (3 Replies)
Discussion started by: Sang
3 Replies

2. Shell Programming and Scripting

Combine logs span across multiple lines

Hi All, I am having a log file ERROR 2016-12-08 10:22:23.542 some data **** some data****** **** some data****** **** some data****** DEBUG 2016-12-08 10:23:23.542 some data **** some data****** **** some data****** **** some data****** when i grep the log file with ERROR am getting... (3 Replies)
Discussion started by: mohanalakshmi
3 Replies

3. Shell Programming and Scripting

Select lines where at least x columns above threshold value

I have a file with 20 columns. I'd like to retain only the lines for which the values in at least x columns, looking only at columns 6-20, are above a threshold. For example, I'd like to retain only the lines in the file below that have at least 8 columns (again, looking only at columns 6-20)... (3 Replies)
Discussion started by: pathunkathunk
3 Replies

4. UNIX for Dummies Questions & Answers

How to randomly select lines from a text file

I have a text file with 1000 lines, I want to randomly select 200 lines from it and print them as output. How do I go about doing that? Thanks! (7 Replies)
Discussion started by: evelibertine
7 Replies

5. Shell Programming and Scripting

Select files by time stamp

Hi, I need help to read file in a directory on basis of time stamp. e.g. If file access in last 2 minutes it should not be copy to remote directory. Below is my script. +++++++++++++++++++++++++ #!/bin/ksh DATE=`date +"%Y-%m-%d_%H%M"` SEPARATER=" " exec < out_interfaces.cfg... (1 Reply)
Discussion started by: qamar.alam
1 Replies

6. Shell Programming and Scripting

need to grep contents of a file within specific time span. regex i am using is not working

Hi , I am trying to extract contents of a file between specified time stamp. but it does not seem to work. i am trying to extract output of /var/adm/messages between 15:00:00 to 15:23:59 . i have tried two regex the first one seems to kind of work. it displays some output. the second one is... (13 Replies)
Discussion started by: chidori
13 Replies

7. Shell Programming and Scripting

How to select/delete some lines in shell?

I need to delete half(approx) the file or select half the file by existence of some character My file looks like 1 2 3 4 . . . 50 . . 100I need to select only 50 to rest of the file or needs to delete the file upto 50. Please help me out.. (6 Replies)
Discussion started by: SujeethP
6 Replies

8. Shell Programming and Scripting

Select lines in which column have value greater than some percent of total file lines

i have a file in following format 1 32 3 4 6 4 4 45 1 45 4 61 54 66 4 5 65 51 56 65 1 12 32 85 now here the total number of lines are 8(they vary each time) Now i want to select only those lines in which the values... (6 Replies)
Discussion started by: vaibhavkorde
6 Replies

9. UNIX for Dummies Questions & Answers

Extracting a Time Span from Syslog Messages File

Hi all, I need your help to explain how I can extract a time span from the syslog messages file on a Solaris 10 system. Here is an example extract of the syslog messages: Dec 4 11:51:38 hajap141-0107.nls.jlrint.com 267938: Dec 4 11:51:36: %DOT11-6-DISASSOC: Interface Dot11Radio0,... (4 Replies)
Discussion started by: wthomas
4 Replies

10. Shell Programming and Scripting

important. select 10 files each time.

Suppose I have a unix file which contain a lost of 60 files like filename1 filename2 ... .. ... filename60 I want to write a unix script that will pick up first 10 files in first run 10-20 files in 2 run 20-30 files in 3 run 30-40 files in 4 run 40-50 files in 5 run 50-60 files in 6... (2 Replies)
Discussion started by: er_zeeshan05
2 Replies
Login or Register to Ask a Question