I have a problem of Finding Day of the week from date, but i need to do it within awk On SOLARIS
Input:20101007(YYYYMMDD)
Output:Thursday
kindly provide suggestions.
Thanks in advance
I have a problem of Finding Day of the week from date, but i need to do it within awk On SOLARIS
Input:20101007(YYYYMMDD)
Output:Thursday
kindly provide suggestions.
Thanks in advance
nawk, not awk
Go here: AWK Language Programming - A Library of awk Functions
Get the code for the mktime() function, which returns epoch seconds from a date. Note: read the article, you need to get several functions, not just one.
The number 0 .. 6 of the day of the week is
dow= (epochseconds / 86400 ) % 7
If you need day names, then create an array and access each element by dow,
day_name[dow]
You can give this a try:
#!/bin/sh
# Day of the week (YYYYMMDD)
d="20101007"
day=${d#??????}
temp=${d#????}
month=${temp%??}
year=${d%????}
DOW=$(cal $month $year | awk '
BEGIN{split("Sun Mon Tue Wed Thu Fri Sat",dow);dow[0]=dow[7]}
NR==3{t=7-NF+d;print dow[t%7];exit}' d=$day)
echo $DOW
Use nawk or /usr/xpg4/bin/awk on Solaris.
If it is current date then you can use :
date +"%A"
Check out the gnu date command from their corutils ,
Examples of date - GNU Coreutils
You can do this:
$ date -d 20101007 "+%A"
Thursday
You can get the output for whatever date you put in after the -d
FreeBSD
# date -j -f "%Y%m%d" "20101007" "+%A"
Thursday
or just
# date -d 20101007 "+%A"
Thursday
Thanks all for you replies:
On my solaris machine date -d doesn't work.
Niether does date -j -f...:((
Franklin52: I have to do this process (dow) for every record in a file, besides other fuctions that are to be performed on other fields of data.
I have written a piece of code as:
awk 'BEGIN{
FS="|";
while(getline < "DAY_MAPPING_NEW.CFG")
{
DAY_NEW[$2]=$1; DAY_NEW[$3]=$1; DAY_NEW[$4]=$1; DAY_NEW[$5]=$1; DAY_NEW[$6]=$1; DAY_NEW[$7]=$1;
}
}
{
dd=substr("'${DATE}'",7,2);
mm=substr("'${DATE}'",5,2);
yyyy=substr("'${DATE}'",1,4);
system("cal "mm" "yyyy" | tail -7|tr \" \" \";\"|tr \"\\n\" \";\" >out.txt");
while(getline < "out.txt")
{OPFILE=$0;}
if (int(dd) <= 9) { dd=substr("'${DATE}'",8,1); }
newdd=";"dd";"
daynum=index(OPFILE,newdd);
print "New Date is:"dd"|"mm"|"yyyy"|"DAY_NEW[daynum];
system("rm out.txt");
OTHER FUNCTIONALITIES
.
.
.
.
..
..
.
.
..
}' INPUTFILE.txt
But i want to get output of system commant in some variable rather than writting o/p in a file "out.txt' and removing it everytime.
Replace these lines:
system("cal ..." >out.txt");
while(getline < "out.txt")
{OPFILE=$0;}
with:
while("cal ..."| getline OPFILE) {..}
Franklin:
I tried following possiblities
while(("cal "month" "year" | tail -7|tr \" \" \";\"|tr \"\\n\" \";\" | getline OPFILE"))
{
print "Here...."OPFILE
}
while(("cal "month" "year" | tail -7|tr \" \" \";\"|tr \"\\n\" \";\"" | getline OPFILE))
{
print "Here...."OPFILE
}
while(system("cal "month" "year" | tail -7|tr \" \" \";\"|tr \"\\n\" \";\"" | getline OPFILE))
{
print "Here...."OPFILE
}
while(system("cal "month" "year" | tail -7|tr \" \" \";\"|tr \"\\n\" \";\" | getline OPFILE"))
{
print "Here...."OPFILE
}
But none worked...