Need help in formatting the date taken from a variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help in formatting the date taken from a variable
# 1  
Old 03-03-2016
Need help in formatting the date taken from a variable

Hi,

I am having the below data in input file. The file contains multiple such lines.
The file is comma delimited.

Code:
AAA,M,CCCCCC,EE,DD,FF,GG,1187.00000,01-MAY-05
BBB,M,CCCCCC,EE,DD,FF,GG,87.00000,10-MAY-05

I need to create below output file out of it-

Code:
<tag1>AAA</tag1>
<date>2005-05-01</date>
<tag1>BBBB</tag1>
<date>2005-05-10</date>


So far i had done below but i need help on formating the date.

Code:
awk 'BEGIN { FS=","; } { printf "<tag1>%s</tag1>\n<date>%s</date>\n", $1,$9}' inputfile


Regards,
Arjun

Last edited by jim mcnamara; 03-03-2016 at 06:26 AM..
# 2  
Old 03-03-2016
How about
Code:
awk '
BEGIN           {for (n=split ("JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC", T); n>0; n--) M[T[n]] = n
                 FS=","
                }
                {split ($9, T, "-")
                 printf "<tag1>%s</tag1>\n<date>20%02d-%02d-%02d</date>\n", $1, T[3], M[T[2]], T[1]
                }
' file
<tag1>AAA</tag1>
<date>2005-05-01</date>
<tag1>BBB</tag1>
<date>2005-05-10</date>

# 3  
Old 03-03-2016
Hello Arjun_CV,

Welcome to forums, please use code tags as per forum rules. For your problem there are following things which I am considering and providing this code.

i- Your month names will be JAN FEB MAR APR MAY JUN JULY AUG SEPT OCT NOV DEC style always into your Input_file.(Else you could have change it according to your requirement too.)
ii- Output which you have shown to us as <tag1>BBBB</tag1>, where BBBB is a typo. As I could see only BBB into shown input.
Following may help you in same.
Code:
awk -F, '{split($NF, array,"-");num=split("JAN FEB MAR APR MAY JUN JULY AUG SEPT OCT NOV DEC", months," ");for(i=1;i<=num;i++){if(months[i]==array[2]){Q=sprintf("%02d", i)}};print "<tag1>" $1 "</tag1>" ORS "<date>" "20" array[3] "-" Q "-" array[1] "</date>"}'   Input_file

Output will be as follows.
Code:
<tag1>AAA</tag1>
<date>2005-05-01</date>
<tag1>BBB</tag1>
<date>2005-05-10</date>

EDIT: Adding a non-one liner form for solution too now.
Code:
 awk -F, '{
                split($NF, array,"-");
                num=split("JAN FEB MAR APR MAY JUN JULY AUG SEPT OCT NOV DEC", months," ");
                for(i=1;i<=num;i++){
                                        if(months[i]==array[2]){
                                                                Q=sprintf("%02d", i)
                                                               }
                                   };
                print "<tag1>" $1 "</tag1>" ORS "<date>" "20" array[3] "-" Q "-" array[1] "</date>"
          }
         '   Input_file

Thanks,
R. Singh

Last edited by RavinderSingh13; 03-03-2016 at 07:22 AM.. Reason: Adding a non-one liner form for solution now too.
# 4  
Old 03-03-2016
Or even
Code:
awk -F, '
                {split ($9, T, "-")
                 printf "<tag1>%s</tag1>\n<date>20%02d-%02d-%02d</date>\n", $1, T[3], (index ("JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC", T[2]) + 3) / 4, T[1]    
                }
' file

This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Need Date Formatting help

Hi, How can i store the date + time from the output of the ls command in loop in a variable date1? -rw-rw---- 1 user1 admin 500002 Jan 2 21:24 P002607.cssI then want to convert Jan 2 21:24 to this date format 2014-01-02 21:24:00 and save it in date2 variable. Then i would like to add... (1 Reply)
Discussion started by: mohtashims
1 Replies

2. Shell Programming and Scripting

date formatting in perl

my code: $dateformat = yyyy_mm_dd if($dateformat =~ m/yyyy/i) { print ("found"); $yyyy = strftime "%Y", localtime; print ("year is $yyyy\n"); $dateformat =~ s/yyyy/$yyyy/; print ("new date format is $dateformat\n"); } elsif($dateformat =~ m/yy/i) { print ("found"); $yy = strftime "%y",... (4 Replies)
Discussion started by: irudayaraj
4 Replies

3. Shell Programming and Scripting

Formatting date

Hi all Y=`date +'%Y'` M=`date +'%m'` D=`date +'%d'` if && ;then yesterday=$Y$M`expr $D + 30` echo $yesterday else if && ; then yesterday=$Y$M`expr $D + 29` echo $yesterday else if ; then yesterday=$Y$M`expr $D + 27` echo $yesterday else yesterday=$Y$M`expr $D - 1` echo... (8 Replies)
Discussion started by: ultimatix
8 Replies

4. Shell Programming and Scripting

Formatting a date

Hi, the date value retrieved by a parameter from the table is of the format dd/mm/yyyy. please let me know how to convert this to YYYYMMDD using sed thanks (4 Replies)
Discussion started by: swasid
4 Replies

5. OS X (Apple)

Date Formatting, etc.

Hi - I'm using GeekTool to customize my desktop in OS X 10.5.8 I'm a complete novice as far as UNIX commands, just know enough to be dangerous. I have a command entered as a Shell to display my events from iCal: This makes my events show something like this: While this is... (1 Reply)
Discussion started by: patricksprague
1 Replies

6. Shell Programming and Scripting

date formatting

Hi i need to have the date in the format like dd-mon-yyyy my script goes like this #!/usr/bin/bash for f in /space/can /home/lbs/current/externalcdrbackup/L_CDR_Configuration/1/200903122* ; do awk '{sum++;}END{for(i in sum) {print d,h,m,i, sum}}' "d=$(date +'%m-%d-%Y')" "h=$(date +'%H')"... (8 Replies)
Discussion started by: aemunathan
8 Replies

7. Shell Programming and Scripting

Formatting Date Variable (Pls help)

I have a situation where I am writing a shell script that will accept a date value it will then pass this date value to an Oracle stored procedure for processing. I want to format the date into (01-SEP-08) before passing to the proc. I also want to make sure the value passed in is a date value... (1 Reply)
Discussion started by: gmoth
1 Replies

8. UNIX for Dummies Questions & Answers

Date formatting

Running bash how do I input the date in the command line like 3/20/90 and get an output formmated like March, 20 1990. (8 Replies)
Discussion started by: knc9233
8 Replies

9. Shell Programming and Scripting

date formatting

Date format MM/DD/YYYY required is YYYYMMDD, I tried using sed but could not get it any help please. (4 Replies)
Discussion started by: mgirinath
4 Replies

10. Shell Programming and Scripting

Formatting date

i need date in the following format December 14, 2005. With date +"%b %d, %Y" command i am getting the following output :- Dec 14, 2005. can anyone pls tell me how to get the full month name (2 Replies)
Discussion started by: radhika03
2 Replies
Login or Register to Ask a Question