Reading in MM/DD/YY, find Day of Week


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Reading in MM/DD/YY, find Day of Week
# 1  
Old 07-21-2014
Reading in MM/DD/YY, find Day of Week

Hi everyone,

I have a shell script that merges many files down in to one, then removes unwanted lines, that part is working fine:

Code:
#!/bin/bash
FILES=/home/pi/temp/qbd/*
for f in $FILES
do
  echo "Processing $f file..."
  # take action on each file. $f store current file name
  echo "Select DateTime, ItemName, AmountGrossIncTax from Transaction where FunctionCaption='Total'" | mdb-sql -p $f >> salesecho
done
  echo "Formatting..."
  sed -e '/Rows/d' -e '/DateTime/d' -e '/^$/d' salesecho > output

The output generated by the script looks like this:

Code:
07/01/14 15:47:22               179.9500
07/01/14 16:03:29               3.9500
06/26/14 11:59:43               19.8000
06/27/14 12:02:43               160.0000
07/02/14 13:12:35               5.9500
07/02/14 13:34:17               5.9500

This is where I get stuck Smilie

I need to parse the file reading the date from each line, and add another column to the line that included the day of the week. So the output I am after would be something like this:

Code:
07/01/14 15:47:22               179.9500      MONDAY
07/01/14 16:03:29               3.9500        MONDAY
06/26/14 11:59:43               19.8000	      WEDNESDAY
06/27/14 12:02:43               160.0000      TUESDAY
07/02/14 13:12:35               5.9500        FRIDAY
07/02/14 13:34:17               5.9500        SATURDAY

Can anyone suggest the best approach? I don't even know where to start.

Many thanks!
# 2  
Old 07-21-2014
Hint, this works in BASH
Code:
date --date="07/01/14" +%u

1-7, where 1 is monday

So here is an example using an associative array to convert number to day of week
Code:
WEEK[1]="MONDAY"; WEEK["2"]="TUESDAY"; WEEK["3"]="WEDNESDAY"; WEEK["4"]="THURSDAY"; WEEK[5]="FRIDAY"; WEEK[6]="SATURDAY"; WEEK[7]="SUNDAY"
DAY_OF_WEEK=`date --date=${1} +%u`
echo "DAY OF WEEK number is : ${DAY_OF_WEEK} and that is day of week: ${WEEK[${DAY_OF_WEEK}]}"

I created that in test script x.sh, so...
Code:
[josephgr@oc0887178221 ~]$ ./x.sh "07/01/14"
DAY OF WEEK number is : 2 and that is day of week: TUESDAY


Last edited by blackrageous; 07-21-2014 at 06:43 PM..
# 3  
Old 07-21-2014
Quote:
Originally Posted by blackrageous
Hint, this works in BASH
Code:
date --date="07/01/14" +%u

1-7, where 1 is monday
Make that
Code:
date --date="07/01/14" +%A

Sunday-Saturday, Where Monday is Monday Smilie

Mike
# 4  
Old 07-21-2014
With gnu awk you can do this fairly efficiently (assumption is all dates are after 1999):

Code:
gawk -F'[\t :/]' '{
t = mktime("20" $3 " " $1 " " $2 " " $4 " " $5 " " $6)
$0=$0 "\t" toupper(strftime("%A", t))
}1' infile

# 5  
Old 07-21-2014
Thank you very much for your help guys.

I've been playing around with a way to get this done with a "while read line" approach:

Code:
#!/bin/bash

while read line
do
    DOW=`awk '{print $1}'`
    date --date=\"$DOW\" +%A
done < output

Unfortunately this does not work, it appears that the while loop processes every line in the file with the awk statement before moving on to the next line, then the date test fails:

Code:
date: extra operand `05/15/14'

Should I be using awk here, or is there a better approach?
# 6  
Old 07-21-2014
Yes there is a better approach.

You can use the shell to get the DATE directly like this:

Code:
#!/bin/bash

while read DATE TIME rest
do
    date --date="$DATE" +%A
done < output

# 7  
Old 07-22-2014
Quote:
Originally Posted by Chubler_XL
Yes there is a better approach.

You can use the shell to get the DATE directly like this:

Code:
#!/bin/bash

while read DATE TIME rest
do
    date --date="$DATE" +%A
done < output

Thanks, but I think you missed the point of my post. I need to convert the date that is contained in the file, in to a day of the week. I do not need to know the current day of the week according to the system.

** EDIT **

I'm an idiot. Thank you very much Chubler_XL, this does indeed work!

I misunderstood how this would work when I read it. Once tried "in anger" it works perfectly.

Thanks again Chubler_XL!

Last edited by gjws; 07-22-2014 at 06:25 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Get the week's day

Hi All, I have the below requirement , if i give the week number for ex 41 i need to get the date for Monday and thursday for this given week. my expected output is 13/10/2014 (Monday's date) and 16/10/2014 (Thursday's date) I am using GNU LINUX . Pls help me with your thoughts. Thanks in... (7 Replies)
Discussion started by: mohanalakshmi
7 Replies

2. UNIX for Dummies Questions & Answers

Sudoers for one day per week?

I have been volunteered by my boss to be the sysadmin for our production redhat server. He asked me to tighten the security to avoid mishaps like "rm -f *" that occured not long ago. Right now, we have 53 users sudo-ing into the machine and it is an audit nightmare. I am wondering if it... (15 Replies)
Discussion started by: alan
15 Replies

3. HP-UX

Find Day of Week

In HP-UX the date command does not have the "-d" switch like some other *nixes do. I'm working a simple script to tell me, given the day, month and year what day of the week that falls on. Assuming valid day, month and year input (I'd perform quality checks on the input separately, but not... (5 Replies)
Discussion started by: rwuerth
5 Replies

4. UNIX and Linux Applications

How to find 'Day of week' in Linux system

Hi All, I want to find a day of week for the Linux system. can some one help me on this.. Thanks in advance, Raji. (2 Replies)
Discussion started by: rajinavaneethan
2 Replies

5. HP-UX

Get Day of Week from date

Hi All, I have date in string format 'YYYY-MM-DD'. I want to know day of the week for this date. Example. For '2005-08-21' my script should return '0' or Sunday For '2005-08-22' it should return '1' or Monday I want piece of code for HP-UX korn shell. Appreciate reply on this. (5 Replies)
Discussion started by: vpapaiya
5 Replies

6. UNIX for Dummies Questions & Answers

How to find Day of the Week from the given date (Perl)?

How to find the Day of the Week of the given Date using perl? If I have a date in YYY--MM-DD format, how to find the DOW? Based on that, I need to find the following sunday. Pls help. (5 Replies)
Discussion started by: deepakwins
5 Replies

7. UNIX for Dummies Questions & Answers

Changing First Day Of The Week?

Hi All, Our system is running on Solaris 8 and we are using US locale. By default the First Day Of Week is Sunday, is it possible for us to change it to Monday? I have googled it but found very little of use. THanks in advance. (2 Replies)
Discussion started by: fowlerleftfoot
2 Replies

8. Shell Programming and Scripting

Yesterday's Day of week

I need o get yesterday's day of week but im not exactly sure. the actual name is what i want. I can do it with numbers but im not sure with words. (3 Replies)
Discussion started by: rcunn87
3 Replies

9. Programming

Function that gets the day of the week (0-6) ??

Hi , I am working at Unix system,using c lang. I need c fun which return the day of the week . For example : 0- Sunday. 1- Monday. .... 10x. (4 Replies)
Discussion started by: kamil
4 Replies

10. UNIX for Dummies Questions & Answers

Calculating the day of the week

Hi all, I would like to calculate the day of the week using a supplied date. i.e. 20011012 = Day 5. Any ideas? Many thanks, ligs (4 Replies)
Discussion started by: ligs
4 Replies
Login or Register to Ask a Question