Perl read file with dates in


 
Thread Tools Search this Thread
Top Forums Programming Perl read file with dates in
# 1  
Old 05-13-2013
Perl read file with dates in

Hi All

I have a text file that has a list of dates in it ( see below example) is there i can just pull out the lines that are from this week ( week starting on monday) and then work out the how many occurances there are on each name in collum 2

Code:
2013-05-13 08:20:02       bacha           Blah              524gps
2013-05-13 08:30:02       guppy           blah              125gps
2013-05-13 12:10:02       mojarra        blah                  104gps
2013-05-13 13:59:01       longfin          blah               105gps

the collums are made using sprintf "%-25s %-15s %-20s %-6s %-20s

many thanks
A

Last edited by Corona688; 05-14-2013 at 04:03 PM..
# 2  
Old 05-13-2013
So, filter for these lines, filter for date range, extract user name and get counts? Shell/sed might be easier, but I am not a PERL fancier. To do the date bit, need input or clock algorythm, your definition of a week, get rid of the hyphens and do integer compare. You can use an associative vector to keep counts or sort|uniq -c.
# 3  
Old 05-14-2013
Code:
$
$ # Current week is from Monday, 5/13/2013 to Sunday, 5/19/2013
$ cat input.txt
2013-05-11 09:10:11 bacha Blah 524gps
2013-05-13 08:20:02 bacha Blah 524gps
2013-05-13 08:30:02 guppy blah 125gps
2013-05-13 12:10:02 mojarra blah 104gps
2013-05-13 13:59:01 longfin blah 105gps
2013-05-14 20:21:22 longfin blah 105gps
2013-05-19 22:23:24 longfin blah 105gps
2013-05-20 09:45:59 longfin blah 105gps
$
$
$ ##
$ perl -lne 'BEGIN {
               use Time::Local 'timelocal_nocheck';
               @x = localtime;
               if ($x[6] == 0) { $sdow = $x[7] - 5;         $edow = $x[7] + 2         }
               else            { $sdow = $x[7] - $x[6] + 2; $edow = $x[7] - $x[6] + 8 }
               @s = localtime timelocal_nocheck 0,0,0,$sdow,0,(1900+$x[5]);
               $s[5] += 1900; $s[4]++;
               $sow = sprintf("%4d%02d%02d",$s[5],$s[4],$s[3]);
               @e = localtime timelocal_nocheck 59,59,23,$edow,0,(1900+$x[5]);
               $e[5] += 1900; $e[4]++;
               $eow = sprintf("%4d%02d%02d",$e[5],$e[4],$e[3]);
             }
             ($y, $mn, $d, $h, $mi, $s, $name) = m/^(\d+)-(\d+)-(\d+)\s+(\d+):(\d+):(\d+)\s+(\w+).*/;
             $curr_dt = sprintf("%4d%02d%02d",$y,$mn,$d);
             if ($sow <= $curr_dt and $curr_dt <= $eow) { $occurrences{$name}++ }
             END {
               while (($k, $v) = each %occurrences) {
                 print "Name : $k\tNo. of occurrences = $v";
               }
             }
            ' input.txt
Name : longfin  No. of occurrences = 3
Name : mojarra  No. of occurrences = 1
Name : bacha    No. of occurrences = 1
Name : guppy    No. of occurrences = 1
$
$

# 4  
Old 05-14-2013
Just mostly off the cuff, not PERL but correct general approach, not tested, narrative follows:
Code:
export lo=20130513 hi=20130519
(IFS="-$IFS"
 declare -A cts 
 while read y m d t u rest
 do
  ymd="$y$m$d"
 
  case "$ymd" in
  ([21][09][0-9][0-9][0-1][0-9][0-3][0-9])
   if (( ymd <= hi && ymd >= lo ))
   then
    if (( ++cts[$u] == 1 ))
    then
     us="$us $u"
    fi
   fi
   ;;
  (*)
   ;;
  esac
 done
 for u in $us
 do
  echo $u $cts[$u]
 done
 
 
 
 
 

)<input_file >cts_file

  1. Store low and high dates in integer form
  2. Open a subshell to segregate code with funny $IFS including dash. (Hope it does not mess up other things!)
  3. Creat an associative array cts for counts.
  4. Begin reading lines into 6 variables: year, month, day, time, user, rest.
  5. If the line starts with a date, increment/create a count for that user.
  6. If this is the first count, save user names in us.
  7. After reading all lines, dump the cts for all users.
I might have left IFS alone and read the date as d, removed the - to create ymd: ymd=`echo $d | tr -d '[-]'` but thats an exec per line, si I need to read bash man to see how to remove them with builtins. I could have neatened up the input with sed so it was only 'ymd u' lines.

Last edited by DGPickett; 05-14-2013 at 04:05 PM..
# 5  
Old 05-21-2013
thanks guys wokrs like s charm
# 6  
Old 05-29-2013
Thanks for this it works great. Can you point out the place in the code where i can adjust the date it looks for.


Thanks
A

Quote:
Originally Posted by durden_tyler
Code:
$
$ # Current week is from Monday, 5/13/2013 to Sunday, 5/19/2013
$ cat input.txt
2013-05-11 09:10:11 bacha Blah 524gps
2013-05-13 08:20:02 bacha Blah 524gps
2013-05-13 08:30:02 guppy blah 125gps
2013-05-13 12:10:02 mojarra blah 104gps
2013-05-13 13:59:01 longfin blah 105gps
2013-05-14 20:21:22 longfin blah 105gps
2013-05-19 22:23:24 longfin blah 105gps
2013-05-20 09:45:59 longfin blah 105gps
$
$
$ ##
$ perl -lne 'BEGIN {
               use Time::Local 'timelocal_nocheck';
               @x = localtime;
               if ($x[6] == 0) { $sdow = $x[7] - 5;         $edow = $x[7] + 2         }
               else            { $sdow = $x[7] - $x[6] + 2; $edow = $x[7] - $x[6] + 8 }
               @s = localtime timelocal_nocheck 0,0,0,$sdow,0,(1900+$x[5]);
               $s[5] += 1900; $s[4]++;
               $sow = sprintf("%4d%02d%02d",$s[5],$s[4],$s[3]);
               @e = localtime timelocal_nocheck 59,59,23,$edow,0,(1900+$x[5]);
               $e[5] += 1900; $e[4]++;
               $eow = sprintf("%4d%02d%02d",$e[5],$e[4],$e[3]);
             }
             ($y, $mn, $d, $h, $mi, $s, $name) = m/^(\d+)-(\d+)-(\d+)\s+(\d+):(\d+):(\d+)\s+(\w+).*/;
             $curr_dt = sprintf("%4d%02d%02d",$y,$mn,$d);
             if ($sow <= $curr_dt and $curr_dt <= $eow) { $occurrences{$name}++ }
             END {
               while (($k, $v) = each %occurrences) {
                 print "Name : $k\tNo. of occurrences = $v";
               }
             }
            ' input.txt
Name : longfin  No. of occurrences = 3
Name : mojarra  No. of occurrences = 1
Name : bacha    No. of occurrences = 1
Name : guppy    No. of occurrences = 1
$
$

# 7  
Old 05-29-2013
Quote:
Originally Posted by ab52
...Can you point out the place in the code where i can adjust the date it looks for. ...
I don't understand.
You want to adjust the date that this code looks for??
You'll have to adjust the date in the input file (data file) itself. Not sure what the point of doing that would be.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl script to read string from file#1 and find/replace in file#2

Hello Forum. I have a file called abc.sed with the following commands; s/1/one/g s/2/two/g ... I also have a second file called abc.dat and would like to substitute all occurrences of "1 with one", "2 with two", etc and create a new file called abc_new.dat sed -f abc.sed abc.dat >... (10 Replies)
Discussion started by: pchang
10 Replies

2. Shell Programming and Scripting

Read/write perl file

Hi I am trying to build a web form where it can take the input from the user and write it to a file. And when I will open that form again that for should read the file that was created at the 1st step and all the fields should auto populate from that file. I have 20 text fields in my form. I... (1 Reply)
Discussion started by: sauravrout
1 Replies

3. Shell Programming and Scripting

Perl write and read on same file

Hi, I am trying to do a write operation followed by a read operation on the same file through Perl, expecting the output produced by read to contain the new lines added, as follows: #! /usr/bin/perl -w open FH, "+< testfile" or die "$@"; print FH "New content added\n"; while (my $line =... (1 Reply)
Discussion started by: royalibrahim
1 Replies

4. Shell Programming and Scripting

Script to read a log file and run 2nd script if the dates match

# cat /tmp/checkdate.log SQL*Plus: Release 11.2.0.1.0 Production on Mon Sep 17 22:49:00 2012 Copyright (c) 1982, 2009, Oracle. All rights reserved. Connected to: Oracle Database 11g Enterprise Edition Release 11.1.0.7.0 - 64bit Production FIRST_TIME NEXT_TIME... (1 Reply)
Discussion started by: SarwalR
1 Replies

5. UNIX for Dummies Questions & Answers

How to write the dates between 2 dates into a file

Hi All, I am trying to print the dates that falls between 2 date variables into a file. Here is the example. $BUS_DATE =20120616 $SUB_DATE=20120613 Output to file abc.txt should be : 20120613,20120614,120120615,20120616 Can you pls help me accomplish this in LINUX. Thanks... (5 Replies)
Discussion started by: dsfreddie
5 Replies

6. Shell Programming and Scripting

Read Data from Config file using Perl

Hi All, Can anyone please explain me how to read data from config file in Perl. Suppose i have a config file named cfile. The data in config file is name=parth lname=mittal user=2007 hostname=fluoride username=parthmittal password=XXXXXX account=unix url=www.unix.com ... (2 Replies)
Discussion started by: parthmittal2007
2 Replies

7. Shell Programming and Scripting

perl: Read array from a flat file

Hello Guru's I want to read an array into a flatfile Please let me know how to do the same So far this the below code use strict; use warnings; open (my $data , '<', $ARGV)|| die "could not open $ARGV:\n$!"; my @array=(<$data>); my @sorted=sort... (8 Replies)
Discussion started by: Pratik4891
8 Replies

8. Shell Programming and Scripting

Read from config file and use it in perl program

Hi, I want to configure some values in config file like below work_dir /home/work csv_dir /home/csv sql_dir /home/sqls reportfirst yes and i want to store each value in variable to use it further in my my perl program ?? any thought on this(i am new to perl) ? ... (2 Replies)
Discussion started by: raghavendra.nsn
2 Replies

9. Shell Programming and Scripting

how to read the contents of a file using PERL

Hi My requirement is to read the contents of a fixed length file and validate the same. But am not able to read the contents of the file and when i tried it to print i get <blank> as an output... I used the below satatements for printing the contents ... (3 Replies)
Discussion started by: meva
3 Replies

10. Shell Programming and Scripting

Read Single Value From File With Perl

Hi all, I have what I would have thought was a very simple problem but I can' find an elegant solution. I have a file which has a single value you in it, say 194. All I want my perl script to do is open the file, read the value and assign that value to a variable. I've done stuff like... (1 Reply)
Discussion started by: Donkey25
1 Replies
Login or Register to Ask a Question