Sponsored Content
Full Discussion: Perl read file with dates in
Top Forums Programming Perl read file with dates in Post 302814527 by ab52 on Wednesday 29th of May 2013 03:38:42 PM
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
$
$

 

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

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

10. 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
Time::localtime(3pm)					 Perl Programmers Reference Guide				      Time::localtime(3pm)

NAME
Time::localtime - by-name interface to Perl's built-in localtime() function SYNOPSIS
use Time::localtime; printf "Year is %d ", localtime->year() + 1900; $now = ctime(); use Time::localtime; use File::stat; $date_string = ctime(stat($file)->mtime); DESCRIPTION
This module's default exports override the core localtime() function, replacing it with a version that returns "Time::tm" objects. This object has methods that return the similarly named structure field name from the C's tm structure from time.h; namely sec, min, hour, mday, mon, year, wday, yday, and isdst. You may also import all the structure fields directly into your namespace as regular variables using the :FIELDS import tag. (Note that this still overrides your core functions.) Access these fields as variables named with a preceding "tm_" in front their method names. Thus, "$tm_obj->mday()" corresponds to $tm_mday if you import the fields. The ctime() function provides a way of getting at the scalar sense of the original CORE::localtime() function. To access this functionality without the core overrides, pass the "use" an empty import list, and then access function functions with their full qualified names. On the other hand, the built-ins are still available via the "CORE::" pseudo-package. NOTE
While this class is currently implemented using the Class::Struct module to build a struct-like class, you shouldn't rely upon this. AUTHOR
Tom Christiansen perl v5.18.2 2013-11-04 Time::localtime(3pm)
All times are GMT -4. The time now is 11:03 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy