Sponsored Content
Top Forums Shell Programming and Scripting Need help in formatting the date taken from a variable Post 302968056 by RavinderSingh13 on Thursday 3rd of March 2016 06:03:51 AM
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.
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
install::TempContent::Objects::mod_perl-2.0.9::docs::apiUserRContribuinstall::TempContent::Objects::mod_perl-2.0.9::docs::api::APR::BucketAlloc(3)

NAME
APR::BucketAlloc - Perl API for Bucket Allocation Synopsis use APR::BucketAlloc (); $ba = APR::BucketAlloc->new($pool); $ba->destroy; Description "APR::BucketAlloc" is used for bucket allocation. "new" Create an "APR::BucketAlloc" object: $ba = APR::BucketAlloc->new($pool); class: "APR::BucketAlloc" arg1: $pool ( "APR::Pool object" ) The pool used to create this object. ret: $ba ( "APR::BucketAlloc object" ) The new object. since: 2.0.00 This bucket allocation list (freelist) is used to create new buckets (via "APR::Bucket->new") and bucket brigades (via "APR::Brigade->new"). You only need to use this method if you aren't running under httpd. If you are running under mod_perl, you already have a bucket allocation available via "$c->bucket_alloc" and "$bb->bucket_alloc". Example: use APR::BucketAlloc (); use APR::Pool (); my $ba = APR::BucketAlloc->(APR::Pool->pool); my $eos_b = APR::Bucket::eos_create($ba); "destroy" Destroy an "APR::BucketAlloc object": $ba->destroy; arg1: $ba ( "APR::BucketAlloc object" ) The freelist to destroy. ret: no return value since: 2.0.00 Once destroyed this object may not be used again. You need to destroy $ba only if you have created it via "APR::BucketAlloc->new". If you try to destroy an allocation not created by this method, you will get a segmentation fault. Moreover normally it is not necessary to destroy allocators, since the pool which created them will destroy them during that pool's cleanup phase. See Also mod_perl 2.0 documentation. Copyright mod_perl 2.0 and its core modules are copyrighted under The Apache Software License, Version 2.0. Authors The mod_perl development team and numerous contributors. perl v5.18.2 2install::TempContent::Objects::mod_perl-2.0.9::docs::api::APR::BucketAlloc(3)
All times are GMT -4. The time now is 02:46 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy