Converting a date to friday date and finding Min/Max date


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Converting a date to friday date and finding Min/Max date
# 8  
Old 07-29-2012
knowing neither date -d nor bash being available everywhere I could not resist:
Code:
while read i; do date -d"$i+$((6-($(date -d$i +%u)+1)%7))days" +%Y%m%d; done <infile

The innermost date yields the day of week (%u) with which, using bash's arithmetic expressions, the difference to next Friday is computed. This then is the input to the outer date -d"sth+xdays" command.
Give it a try!
# 9  
Old 07-30-2012
Hi Neutronscott,

Above both codes are not wokring Smilie...

awk: 0602-553 Function mktime is not defined.
The input line number is 1. The file is test.
The source line number is 1.

if the input is as below in a file,
20120711(Wed)
20120719(Thu)
20120721(sat)

Then my output should be like this in another file,
20120713
20120720
20120727(Friday after the saturday)

and Min date(20120713) and Max date(20120727) should come in another file.

My box is AIX...and the shell is Korn shell...kindly help Smilie

---------- Post updated at 05:23 PM ---------- Previous update was at 05:16 PM ----------

Dear RudiC,

i got the below error Smilie

Code:
date: Not a recognized flag: d
Usage: date [-u] [+"Field Descriptors"]
date: Not a recognized flag: d
Usage: date [-u] [+"Field Descriptors"]
date: Not a recognized flag: d
Usage: date [-u] [+"Field Descriptors"]
date: Not a recognized flag: d
Usage: date [-u] [+"Field Descriptors"]
date: Not a recognized flag: d
Usage: date [-u] [+"Field Descriptors"]
date: Not a recognized flag: d
Usage: date [-u] [+"Field Descriptors"]


Last edited by rbatte1; 12-21-2017 at 01:21 PM..
# 10  
Old 07-30-2012
As expected, unfortunately. Here's a collection of date manipulation ideas:

https://www.unix.com/answers-frequent...rithmetic.html
This User Gave Thanks to RudiC For This Post:
# 11  
Old 07-30-2012
date math is rather interesting, and i read some of those threads RudiC suggested yesterday. unfortunately, my understanding of ksh is not as well as bash. i was unable to get the functions operating the same:

Code:
$ printf '%s\n' 20120711 20120719 20120721 | bash ./julian
2012/07/11 (jd:2456120 dow:2) + 2 = 20120713
2012/07/19 (jd:2456128 dow:3) + 1 = 20120720
2012/07/21 (jd:2456130 dow:5) + 6 = 20120727
$ printf '%s\n' 20120711 20120719 20120721 | ksh ./julian
2012/07/11 (jd:2456120 dow:2) + 2 = 20120712
2012/07/19 (jd:2456130 dow:5) + 6 = 20120801
2012/07/21 (jd:2456130 dow:5) + 6 = 20120801

Smilie

probably someone wiser could do it easier by parsing the output of cal(1) but the awk code did produce same results across the awks which i have.

Code:
$ printf '%s\n' 20120711 20120719 20120721 | awk -f julian.awk
20120713
20120720
20120727

Min: 20120713
Max: 20120727

Code:
#!/usr/bin/awk -f
function mk_j(y,m,d,    a, j) {
        a=int((14-m)/12);y=y+4800-a;m=m+12*a-3
        return d+int((153*m+2)/5)+365*y+int(y/4)-int(y/100)+int(y/400)-32045
}
function mk_g(jd,       l,n,i,j,d,m,y) {
        l=jd+68569;n=int((4*l)/146097);l=l-int((146097*n+3)/4)
        i=int(4000*(l+1)/1461001);l=l-int((1461*i)/4)+31
        j=int((80*l)/2447);d=l-int((2447*j)/80);l=int(j/11);m=j+2-(12*l);
        y=100*(n-49)+i+l;return sprintf("%04d%02d%02d",y,m,d)
}
function next_fri(jd,   dow) { dow=jd%7;return mk_g(jd+((dow>4)?7:0)+(4-dow)) }
1 { f=0;y=substr($f,1,4);m=substr($f,5,2);d=substr($f,7,2);
  dates[++i]=mk_j(y,m,d);print next_fri(dates[i])
}
END {
        min=max=dates[1]
        for (i in dates) {
                if (dates[i] > max) max=dates[i]
                if (dates[i] < min) min=dates[i]
        }
        printf("\nMin: %s\nMax: %s\n",
                next_fri(min), next_fri(max));
}

you can redirect the output of the min/max to a different file like this if you wish
Code:
printf("\nMin: %s\nMax: %s\n", next_fri(min), next_fri(max)) > "minmax"

This User Gave Thanks to neutronscott 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

Compare date in .txt with system date and remove if it's lesser than system date

Can someone help me with the code wherein there is a file f1.txt with different column and 34 column have expiry date and I need to get that and compare with system date and if expiry date is <system date remove those rows and other rows should be moved to new file f2.txt . I don't want to delete... (2 Replies)
Discussion started by: Stuti
2 Replies

2. Shell Programming and Scripting

Date: invalid date trying to set Linux date in specific format

i try to set linux date & time in specific format but it keep giving me error Example : date "+%d-%m-%C%y %H:%M:%S" -d "19-01-2017 00:05:01" or date +"%d-%m-%C%y %H:%M:%S" -d "19-01-2017 00:05:01" keep giving me this error : date: invalid date ‘19-01-2017 00:05:01' Please use CODE tags... (7 Replies)
Discussion started by: umen
7 Replies

3. Shell Programming and Scripting

Converting String Date into UNIX Date

Hi, I have a string date to my unix script(sun solaris). I wanted to convert it into unix date so that I can use it in a conditional statement. Please see below: MyTest.sh -s 2018-05-09 suppdt=$1 # string date passed via arguement as 2018-04-09 curryr=`date '+%Y'` nextyr=`expr... (2 Replies)
Discussion started by: Saanvi1
2 Replies

4. Red Hat

How to find/display out last Friday's date of the month?

Hello, Can you please help me find/display out last Friday's date of the month using command in Unix/Linux (3 Replies)
Discussion started by: sunnysthakur
3 Replies

5. Shell Programming and Scripting

finding date numeral from file and check the validity of date format

hi there I have file names in different format as below triss_20111117_fxcb.csv triss_fxcb_20111117.csv xpnl_hypo_reu_miplvdone_11172011.csv xpnl_hypo_reu_miplvdone_11-17-2011.csv xpnl_hypo_reu_miplvdone_20111117.csv xpnl_hypo_reu_miplvdone_20111117xfb.csv... (10 Replies)
Discussion started by: manas_ranjan
10 Replies

6. Shell Programming and Scripting

To get max/min Date/Timestamp from a file

I want to get maximum/minimum date/timestamp from a data file ? Sample Input File ============= rec#,order_dt,ext_ts 1,2010-12-01,2010-12-01 17:55:23.222222 2,2011-11-05,2010-12-01 19:55:23.222222 3,2009-10-01,2010-12-01 18:55:23.222222 for above file Maximum Order_dt = 2011-11-05... (5 Replies)
Discussion started by: vikanna
5 Replies

7. Shell Programming and Scripting

finding the previous day date and creating a file with date

Hi guys, I had a scenario... 1. I had to get the previous days date in yyyymmdd format 2. i had to create a file with Date inthe format yyyymmdd.txt format both are different thanks guys in advance.. (4 Replies)
Discussion started by: apple2685
4 Replies

8. Shell Programming and Scripting

Date One Week Ago From Given Date, Not From Current Date

Hi all, I've used various scripts in the past to work out the date last week from the current date, however I now have a need to work out the date 1 week from a given date. So for example, if I have a date of the 23rd July 2010, I would like a script that can work out that one week back was... (4 Replies)
Discussion started by: Donkey25
4 Replies

9. Shell Programming and Scripting

Get the date of monday when running from Friday to next Thursday

Hi, I have a requirement where I want to get the date of monday when I am running the script from previous Friday to the following Thursday. For example: When ever I run the script between 19thFeb2010(Friday) to 25th Feb 2010(Thursday), I should get the date of 22nd Feb 2010 in the format of... (5 Replies)
Discussion started by: fasiazhar_411
5 Replies

10. HP-UX

a simple way of converting a date in seconds to normal date

Hi all! I'm working on a HPUX system, and I was wondering if there is a simple way to convert a date from seconds (since 1970) to a normal date. Thanks (2 Replies)
Discussion started by: travian
2 Replies
Login or Register to Ask a Question