The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Advanced & Expert Users
.
google unix.com



UNIX for Advanced & Expert Users Expert-to-Expert. Learn advanced UNIX, UNIX commands, Linux, Operating Systems, System Administration, Programming, Shell, Shell Scripts, Solaris, Linux, HP-UX, AIX, OS X, BSD.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Simple question (for you guys, hard for me) BkontheShell718 Shell Programming and Scripting 3 01-07-2008 11:48 AM
[question] hard exercise, help needed EnioMarques UNIX for Dummies Questions & Answers 2 09-20-2007 10:22 PM
Hard Drive Question Netghost AIX 0 08-24-2006 01:01 PM
Please Help.... Desperate need! Hard Question! Sparticus007 UNIX for Advanced & Expert Users 1 01-11-2002 06:43 PM
Experts Only! Hard Question Ahead!!!! Foo49272 UNIX for Advanced & Expert Users 1 01-07-2002 10:22 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-01-2008
msb65 msb65 is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 89
hard question

I have a directory containing a series of files of the format:
A2008001231000.L2

I only care about the 6-8 digits, so the files are effectively:
?????---*.L2

I have files that range from ?????001*.L2 to ?????366*.L2

It should be noted these three digits represent the julian day of the files. My goal is to be able to define two variables: start_day and end_day. How can I list off all the files between start_day and end_day. NOTE: Since the julian days must be 3 digits long, it complicates matters.
  #2 (permalink)  
Old 08-02-2008
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,100
If you think this is hard, you must be using the wrong language. Here it is in ksh. What you call "julian" I will call "day of year". Look up "julian day" on wikipedia. This script uses my datecalc script to allow the user to enter the endpoints as yyyymmdd. datecalc is posted to this site and our search function can find it for you.

One thing about ksh... a number with a leading zero is assumed to be octal. This is why I keep stripping leading zeros off numbers before doing arithmetic.

Code:
$ ls -1 files
A2008001231000.L2
A2008021231000.L2
A2008041231000.L2
A2008061231000.L2
A2008081231000.L2
A2008101231000.L2
$
$
$
$ ./findit
enter start yyyymmdd - 20080101
enter end yyyymmdd - 20080220
A2008001231000.L2 is in range
A2008021231000.L2 is in range
A2008041231000.L2 is in range
$
$
$
$ cat findit
#! /usr/bin/ksh

read start?"enter start yyyymmdd - "
y1=${start%????}
d1=${start#??????}
temp=${start%$d1}
m1=${temp#$y1}
m1=${m1#0}
d1=${d1#0}
doy1=$(($(datecalc -a $y1 $m1 $d1 - $y1 1 1) + 1))
#echo $start $y1 $m1 $d1 $doy1

read end?"enter end yyyymmdd - "
y2=${end%????}
d2=${end#??????}
temp=${end%$d2}
m2=${temp#$y2}
m2=${m2#0}
d2=${d2#0}
doy2=$(($(datecalc -a $y2 $m2 $d2 - $y2 1 1) + 1))
#echo $start $y2 $m2 $d2 $doy2

cd files
ls | while read name ; do
        temp=${name#?}
        temp1=${temp#????}
        f_year=${temp%$temp1}
        temp2=${temp1#???}
        f_doy=${temp1%$temp2}
        f_doy=${f_doy##*(0)}
#       echo $name $f_year $f_doy
        if ((y1<=f_year && doy1<=f_doy && f_year<=y2 && f_doy<=doy2)) ; then
                echo $name is in range
        fi
done
exit 0
$
  #3 (permalink)  
Old 08-02-2008
redoubtable redoubtable is offline
Registered User
  
 

Join Date: Aug 2008
Location: Portugal
Posts: 242
I see you're in mktime() paradise.
From the man mktime:
Code:
           struct tm {
               int tm_sec;         /* seconds */
               int tm_min;         /* minutes */
               int tm_hour;        /* hours */
               int tm_mday;        /* day of the month */
               int tm_mon;         /* month */
               int tm_year;        /* year */
               int tm_wday;        /* day of the week */
               int tm_yday;        /* day in the year */
               int tm_isdst;       /* daylight saving time */
           };

 tm_yday
              The number of days since January 1, in the range 0 to 365.
Well, I couldn't get mktime() to work directly, but I still used tm_yday.
Code:
Tsunami julian_days # perl test2.pl 
Enter start date [yyyy/mm/dd]: 20080101
Enter end date [yyyy/mm/dd]: 20080220
A2008041231000.L2
A2008021231000.L2
A2008001231000.L2
Tsunami julian_days # perl test2.pl 
Enter start date [yyyy/mm/dd]: 2008/01/01  
Enter end date [yyyy/mm/dd]: 2008/02/20
A2008041231000.L2
A2008021231000.L2
A2008001231000.L2
Tsunami julian_days #
and the code:

Code:
#!/usr/bin/perl -w

use POSIX;

$| = 1;

my $input;
my (@date_s, @date_e);

opendir(DIR, ".") || die "can't opendir: $!";
@dots = grep { ! /^\./ && /\.L2$/ } readdir(DIR);
closedir DIR;

print "Enter start date [yyyy/mm/dd]: ";
($input = <STDIN>) =~ /(\d{4})\/*-*(\d{2})\/*-*(\d{2})/;
$date_s[0] = mktime (0, 0, 0, $3, ($2-1), ($1-1900), 0, 0);
$date_s[2] = $1;
print "Enter end date [yyyy/mm/dd]: ";
($input = <STDIN>) =~ /(\d{4})\/*-*(\d{2})\/*-*(\d{2})/;
$date_e[0] = mktime (0, 0, 0, $3, ($2-1), ($1-1900), 0, 0);
$date_e[2] = $1;
(undef, undef, undef, undef, undef, undef, undef, $date_s[1] ,undef) = localtime($date_s[0]);
(undef, undef, undef, undef, undef, undef, undef, $date_e[1] ,undef) = localtime($date_e[0]);

foreach my $i (@dots)
{
        $i =~ /^A(\d{4})(\d{3})/;
        print $i . "\n" if (($1 >= $date_s[2] && $1 <= $date_e[2]) && (($2+0) >= $date_s[1] && ($2+0) <= $date_e[1]));  
}
Sponsored Links
Closed Thread

Bookmarks

Tags
datecalc

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 10:51 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0