LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-09-2010, 10:40 AM   #1
mgsmith7475
LQ Newbie
 
Registered: Mar 2008
Location: Michigan
Distribution: RHEL 5.4 / Ubuntu 9.10
Posts: 8

Rep: Reputation: 0
Help, converting date format in a comma separated field!


I am in the process of learning some scripting, however I am running into a roadblock in specifying a certain time format in the array. Ideally I would like to use "%I:%M %p %A"

Here are the lines of text that I am interrogating:

1123,3/25/2010,00:14 Thu,33229
1124,3/26/2010,13:30 Fri,33230
1125,3/27/2010,04:49 Sat,33231

The script I have is reading each line one by one and sending each row out as an email to a voice mailbox that is automatically read to the user at login. The 3rd field is in a 24 hour format and I would like it in a 12 hour format with AM or PM associated. I can use sed against the 3 character day representation without any trouble, but the current 24 hour format of hh:mm is giving me heartburn at the moment. Here is the script that I have thus far, which does work for the most part.

Please help!!

#!/bin/bash -vx

MAIL="@modmsg-mss1.testdomain.com"
IFS=$(echo -en "\n\b")

#Setup reg-ex for CSV
q='"' # quote
w=$' \t' # whitespace
nq="^$q" # not quote
nwc="^$w," # not whitespace or comma
nwqc="^$w$q," # not whitespace, quote or comma
date=$(date '+%x')
uqf="[$nwqc]([$w]*[$nwc])*" # unquoted field
qf="($q[$nq]*$q)*" # quoted field
iqf="($q[$nq]*$q)*$q[$nq]*" # incomplete quoted field

csv="[$w]*($qf|$uqf)[$w]*,[$w]*(.*)" # comma separated values

# Print fields[] arrays along with their indices - testing purpose only.

function csvprint () {
for i in ${!fields[*]}
do
printf '%s\n' "${fields[i]}"
done
}

unset fields
nf=0
#Get File Name
fname=$1
#Setup Messages
msg1="Welcome! "
msg2="Your delivery is Scheduled for "
msg3=" at "
msg4=" military time."
msg5=" Have a Great Day! "
#Check if file exists
if [ ! -f $fname ];then
echo "File $fname not present - exiting"
exit 2
fi

#Open File
exec<$fname

#Process all lines in file
while read line
do
while [[ "$line" =~ ^$csv$ ]]
do
fields[nf++]="${BASH_REMATCH[1]}"
line="${BASH_REMATCH[4]}"
done
fields[nf++]=$line;

fi
#Create Mail message body
#mailmsg=$msg1${fields[0]}$msg2${fields[1]}$msg3
mailmsg=$msg1$msg2${fields[1]}$msg3${fields[2]}$msg4$msg5
#Create send to info
mailto=$line$MAIL
#E-mail information to specific mailbox
echo $mailmsg|mail -s "Your Delivery Information for Restaurant ${fields[0]}" $mailto -- -f "deliveries@testdomain.com"
nf=0;
unset fields
done
exit 0

Last edited by mgsmith7475; 04-09-2010 at 10:43 AM.
 
Old 04-09-2010, 11:00 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Sorry, I'm a bit tired now to go through your script, but using the GNU date command you can do something like this:
Code:
$ cat file
1123,3/25/2010,00:14 Thu,33229
1124,3/26/2010,13:30 Fri,33230
1125,3/27/2010,04:49 Sat,33231
$
$ awk -F, '{print $2, $3}' file | date -f- +"%I:%M %p %A"
12:14 AM Thursday
01:30 PM Friday
04:49 AM Saturday
This is due to the fact that "m/dd/yyyy HH:MM Day" is a valid input date format. You can easily change the part before the pipe to parse one line at a time in a for loop.

Hope this helps.
 
Old 04-09-2010, 11:34 AM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
An additional note: another way to manage dates is by means of awk itself (using the mktime and strftime statements). In this way you can easily print out more data from the input file. Here is an example:
Code:
$ awk -F, '{
>   split($2,date,"/")
>   split($3,time," ")
>   daystr = (date[3] " " date[1] " " date[2] " " gensub(/:/," ",1,time[1]) " 00")
>   day = mktime(daystr)
>   printf "Your delivery is Scheduled for %s at %s military time. Have a great day!\n", $2, strftime("%I:%M %p %A", day)
> }' file
Your delivery is Scheduled for 3/25/2010 at 12:14 AM Thursday military time. Have a great day!
Your delivery is Scheduled for 3/26/2010 at 01:30 PM Friday military time. Have a great day!
Your delivery is Scheduled for 3/27/2010 at 04:49 AM Saturday military time. Have a great day!
Obviously in your case, better to stick with the date command which is more flexible and easy to manage. This is just to illustrate another chance.

Last edited by colucix; 04-09-2010 at 11:37 AM.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Parsing a comma separated CSV file where fields have commas in to trickyflash Linux - General 7 03-26-2009 03:30 PM
How to delete Comma in a comma separated file with double quotes as quote character pklcnu Linux - Newbie 2 03-24-2009 05:50 PM
help with comma separated values and what should be a simple script. zaber Programming 10 03-06-2008 12:58 PM
bash syntax: looping through a comma-separated list David the H. Linux - General 10 09-06-2007 10:23 AM
Reading comma-separated data from file MeLassen Programming 4 04-04-2004 02:41 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:47 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration