Help, converting date format in a comma separated field!
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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:
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.
#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
# 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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.