Extracting Dates


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Extracting Dates
# 1  
Old 11-04-2013
Extracting Dates

I need to generate a file which should have range of year and month in the below mentioned format:

Code:
200901
200902
.
.
.
till 
201311

This jobs runs on-demand. Even if this is ran twice a month, only one entry should be maintained for particular year and month.
ie . If this jobs runs twice this month only one entry of 201311 must be maintained in that file having other entries undisturbed

Last edited by Scott; 11-04-2013 at 07:18 AM.. Reason: Moved thread; Not an AIX-specific question
# 2  
Old 11-04-2013
Are you simply trying to generate a list of year-month pairs between two dates?
# 3  
Old 11-04-2013
Do you just want to add the current YYYYMM to a file whenever the script runs?

Something like:
Code:
FILENAME=whatever.txt
DATESTR=$(date +%Y%m)
grep $DATESTR "$FILENAME" > /dev/null || echo $DATESTR >> "$FILENAME"

(Assuming it's always going to be added at the end)
# 4  
Old 11-05-2013
Yes, I need to generate a list of year-month pairs between two dates.
# 5  
Old 11-05-2013
Try
Code:
{ cat file; date +%Y%m; } | uniq

# 6  
Old 11-05-2013
I wrote this shell script that can take a given start\end date in year month format (i.e. 200901) and generate a list. Feel free to use it or pick it apart to get what you need:

Code:
#!/bin/bash
#
# --genDate.sh
#
# -- script will take a start and end date in format
# -- yyyymm and generate a list beginning with the
# -- starting date until the end date
#

# prompt for the start and end dates
read -p "Enter start date in yyyy/mm format: " startDate
read -p "  Enter end date in yyyy/mm format: " endDate

# set counter
counter=1

# set newdate var
newDate=0

# reformat the start date to mm/dd/yyyy format
# to make the date command happy
newStartDate=$(echo $startDate | awk 'BEGIN{FIELDWIDTHS="4 2"}{print $2"/01/"$1}')

# generate date list
echo
echo "Generating date list from $startDate to $endDate:"
echo
sleep 1
echo "$startDate"
while [ $newDate != $endDate ]
do
    newDate=$(date --date="$newStartDate + $counter month" +%Y%m)
    echo "$newDate"
    ((counter=$counter+1))
done
echo

# done
exit 0

./genDate.sh
Enter start date in yyyy/mm format: 200901
  Enter end date in yyyy/mm format: 201001

Generating date list from 200901 to 201001:

200901
200902
200903
200904
200905
200906
200907
200908
200909
200910
200911
200912
201001

55172a01e23d6b32df90074ee6b82443

Last edited by in2nix4life; 11-12-2013 at 01:47 PM..
# 7  
Old 11-06-2013
It gives the below error
Code:
date: illegal option -- -
Usage: date [-u] [+Field Descriptors]
date.sh[14]: test: argument expected

My env is pure ksh, I doubt that about bash executing here.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Display dates between two dates

Hi All, I have 2 dates in mm/dd format. sdate=10/01 (October 01) edate=10/10 (October 10) I need the dates in between these 2 dates like below. 10/01 10/02 10/03 10/04 10/05 10/06 10/07 10/08 (1 Reply)
Discussion started by: jayadanabalan
1 Replies

2. Shell Programming and Scripting

How to get dates between two dates?

HI, to my shell script i pass two parameters date1 and date2 is there any way to get all dates betwen these two dates? if i pass 20130714 and 20130717 i need to get below dates 20130715 20130716 is it possible. thanks (3 Replies)
Discussion started by: ulab
3 Replies

3. UNIX for Advanced & Expert Users

How to get the Missing dates between two dates in the table?

Hi Am Using Unix Ksh ... I have a Table called date select * from date ; Date 01/02/2013 06/02/2013 I need the output as Missing Date 01/02/2013 02/02/2013 03/02/2013 04/02/2013 05/02/2013 06/02/2013 (2 Replies)
Discussion started by: Venkatesh1
2 Replies

4. Shell Programming and Scripting

Generating dates between two dates

HI, i have row like this HHH100037440313438961000201001012012073110220002 N in this i have 2 dates in pos 25-32 and 33-40 , so based upon the se two dates , i need to generated records between these two values so in the above record 20100101 and 20120731 need to genearte rows like this... (4 Replies)
Discussion started by: sathishsr
4 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. Emergency UNIX and Linux Support

Replacing dates]] with (dates)]]

Hi guys, For my wiki site I need to fix 1400 pages that use the wrong date format, most pages (not all) use eg. 1988]] I need to change that to (1988)]] The date range goes back to 1400 so I guess I need to do the following ssh into my server, dump mysql database vi .sql dump search... (20 Replies)
Discussion started by: lawstudent
20 Replies

7. Programming

SQL: find if a set od dates falls in another set of dates

Don't know if it is important: Debian Linux / MySQL 5.1 I have a table: media_id int(8) group_id int(8) type_id int(8) expiration date start date cust_id int(8) num_runs int(8) preferred_time int(8) edit_date timestamp ON UPDATE CURRENT_TIMESTAMP id... (0 Replies)
Discussion started by: vertical98
0 Replies

8. Programming

Dates in C

Hello, I am working on a program in C and need to read, write and manage Dates (YYY-MM-DD HH:MM). I have made my own version of a structure to store the date data and something else. Do you recommend me to use the time.h library? In such case, is it worthwhile to have an structure containing... (1 Reply)
Discussion started by: Ister
1 Replies

9. Shell Programming and Scripting

Need script to generate all the dates in DDMMYY format between 2 dates

Hello friends, I am looking for a script or method that can display all the dates between any 2 given dates. Input: Date 1 290109 Date 2 010209 Output: 300109 310109 Please help me. Thanks. :):confused: (2 Replies)
Discussion started by: frozensmilz
2 Replies

10. Shell Programming and Scripting

Difference between two dates...

Hi All, Wish you a Happy New year... I have to find the difference between two dates, the result should be the number of days. I have seen the "datecalc" function. Its good, can I have any other alternative. Thanks in Advance Raju (4 Replies)
Discussion started by: rajus19
4 Replies
Login or Register to Ask a Question