LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-23-2012, 10:20 PM   #1
casperdaghost
Member
 
Registered: Aug 2009
Posts: 349

Rep: Reputation: 16
reformatting date commands.


I have a shell program which spits out a date in this format 12012011 (dec 1st 2011)

I need to reformat it into a m/d/y format but the date command does not like the format.

It likes 20111201 just fine however does not like 12012011

Is there a bash hacker way around this?

Code:
shortcasper@shortcasper-laptop:~/work$ date --date 12012011 +'%m/%d/%Y'
date: invalid date `12012011'
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 01-24-2012, 12:09 AM   #2
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 370Reputation: 370Reputation: 370Reputation: 370
If you need to limit yourself to tools native to bash alone, then this is one possibility:
Code:
od="12012011" ; date --date ${od:4:4}${od:0:4} +'%m/%d/%Y'
I use "od" ("old date"/"original date") to keep the variable name short. The rest is just using bash's parameter expansion special cases to move the pieces around.

If you can use other binaries instead of native bash tools alone, then sed can rearrange the date as well:
Code:
echo "12012011" | sed 's@\([0-9]\{4\}\)\([0-9]\{4\}\)@\2\1@'
EDIT:
I should point out that both of the approaches above require an eight-character date to work properly. In other words, a date of 2012012 (Feb. 1, 2012)--missing the the leading 0--will not work.

EDIT2:
Now that I think about it, there really doesn't seem to be any need to use the date command at all:
Code:
od="12012011" ; echo "${od:0:2}/${od:2:2}/${od:4}"
or
Code:
echo "12012011" | sed 's@\([0-9]\{2\}\)\([0-9]\{2\}\)\([0-9]\{4\}\)@\1/\2/\3@'
12/01/2011

Last edited by Dark_Helmet; 01-24-2012 at 12:29 AM.
 
2 members found this post helpful.
Old 01-24-2012, 03:10 AM   #3
casperdaghost
Member
 
Registered: Aug 2009
Posts: 349

Original Poster
Rep: Reputation: 16
I have this date underscore session combination - represented by the $switch. It is the key to working with many of the programs at work
We assign a unique session to the date - there can be many sessions in one day - this
date_session combinations helps us sort out one job from another.

I need to write scripts that interact with this unique key combination.
I subsrt the date from the $switch and need to push the date out in different formats for different reasons - like report writing.
Different reports need different date formats.
Code:
#!/usr/bin/perl

$switch="12012011_000005000";
$the_date= substr($switch, 0,8);
$format_date= `/bin/date --date $the_date +'%m\/%d\/%Y'`;
print "This the unslashed date $the_date\n";
print "$format_date\n";


shortcasper@shortcasper-laptop:~/work$ ./test_date
date: invalid date `12012011'
This is unslashed date 12012011
i have plugged different values into the $the_date - the date function likes 20111201, but does not like
the 12012011.

In this case - All i need to do is switch the 2011 from the last four digits to the first four digits - change the 12012011 to 20111201. I f it can be done in sed, i suppose it can be done in Perl

Last edited by casperdaghost; 01-24-2012 at 03:20 AM.
 
Old 01-24-2012, 03:26 AM   #4
casperdaghost
Member
 
Registered: Aug 2009
Posts: 349

Original Poster
Rep: Reputation: 16
both of these work great from the command line!!:
Code:
casper@casperlaptop:~/work$ echo "12012011" | sed 's@\([0-9]\{4\}\)\([0-9]\{4\}\)@\2\1@'
20111201
casper@casperlaptop:~/work$ od="12012011" ; date --date ${od:4:4}${od:0:4} +'%m/%d/%Y'
12/01/2011
shortcasper@shortcasper-laptop:~/work$
but fail when plugged into scripts -


Code:
#!/usr/bin/perl

$switch="12012011_000005000";
$the_date= substr($switch, 0,8);
$format_date= `date --date ${the_date:4:4}${the_date:0:4} +'%m/%d/%Y'`;
print "$the_date\n";
print "$format_date\n";

Last edited by casperdaghost; 01-24-2012 at 03:27 AM.
 
Old 01-24-2012, 04:30 AM   #5
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 370Reputation: 370Reputation: 370Reputation: 370
To be honest, I'm not much of a Perl guy. In a sec, I'll do a quick search or two. There most certainly will be a way to do it in perl.

As for the two commands. It's not surprising they don't work in the perl script. Those two were written from the perspective of a bash shell script, but you're target script is perl. Bash has a very different syntax and much more restricted functionality (as compared to perl). It was my impression you were looking for a bash solution because you asked for "a bash hacker way" in your original question.

If my Google searching skills have not failed me, this should work:
Code:
#!/usr/bin/perl

$switch="12012011_000005000";
$the_date=substr($switch,4,4) . substr($switch,0,4);
$format_date=substr($switch,0,2) . "/" . substr($switch,2,2) . "/" . substr($switch,4,4);

print "$the_date\n";
print "$format_date\n";
There might be a more "clever" way to do it with near-indecipherable syntax (as is my experience with perl), but the above is relatively obvious what it's doing in case anyone needs to review it months from now...

Last edited by Dark_Helmet; 01-24-2012 at 04:35 AM.
 
1 members found this post helpful.
Old 01-24-2012, 05:00 AM   #6
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 242Reputation: 242Reputation: 242
Code:
$switch="12012011_000005000";
$date = $format_date = (split /_/, $switch)[0];
$format_date =~ s|(\d{2})(\d{2})(\d{4})|$1/$2/$3|;

print "This the unslashed date $date\n";
print "$format_date\n";
edit: not neccessarly clever, just another way to do it

Last edited by Cedrik; 01-24-2012 at 07:37 AM.
 
1 members found this post helpful.
Old 01-24-2012, 12:45 PM   #7
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 370Reputation: 370Reputation: 370Reputation: 370
Quote:
Originally Posted by Cedrik
not neccessarly clever, just another way to do it
Heh. Please, by all means, casperdaghost should see approaches from people more experienced with perl. Like I said, I'm not much of a perl guy.

My "clever" comment was born from my perl-bitterness when it comes to readability (or lack thereof) of perl commands

One thing though (and given the above, I hope it's not taken the wrong way), but I think your commands are missing one element of casper's goal: your $date variable does not have the 2011 moved from the end of $switch to the beginning.
 
1 members found this post helpful.
Old 01-24-2012, 05:40 PM   #8
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,258

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
Quote:
Originally Posted by Dark_Helmet View Post
EDIT2:
Now that I think about it, there really doesn't seem to be any need to use the date command at all:
Code:
od="12012011" ; echo "${od:0:2}/${od:2:2}/${od:4}"
This is the approach I would take, be it sh, bash, perl, or any other scripting language. There is absolutely no reason to call the date command, just pull out the individual pieces of the date (month, day, year) and rearrange them as necessary using string manipulation.
 
1 members found this post helpful.
Old 01-24-2012, 06:25 PM   #9
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 242Reputation: 242Reputation: 242
Quote:
Originally Posted by Dark_Helmet View Post
Heh. Please, by all means, casperdaghost should see approaches from people more experienced with perl. Like I said, I'm not much of a perl guy.

My "clever" comment was born from my perl-bitterness when it comes to readability (or lack thereof) of perl commands

One thing though (and given the above, I hope it's not taken the wrong way), but I think your commands are missing one element of casper's goal: your $date variable does not have the 2011 moved from the end of $switch to the beginning.
I get it

Code:
#!/usr/bin/perl

$switch = "12012011_000005000";

my ($month, $day, $year) = $switch =~ /(\d{2})(\d{2})(\d{4})/;

print 'date: ' . $year . $month . $day . "\n";
print 'formated date: ' . $month . '/' . $day . '/' . $year . "\n";

Last edited by Cedrik; 01-24-2012 at 06:35 PM.
 
1 members found this post helpful.
Old 01-24-2012, 10:32 PM   #10
casperdaghost
Member
 
Registered: Aug 2009
Posts: 349

Original Poster
Rep: Reputation: 16
hey thanks guys - i have a job to do and need this bad.

the gangs at stackoverflow and perlmonks would crucify me for asking this question.

you guys just solved it - without prejudice.
 
Old 01-24-2012, 11:00 PM   #11
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
Another Perlism
Code:
$var1='12012011';   # mmddyyyy
($var2,$var3,$var4) = unpack("A2A2A4", $var1);
print "$var2,$var3,$var4\n";
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Date comparison with 'string date having slashes and time zone' in Bash only TariqYousaf Programming 2 10-08-2009 08:37 AM
[SOLVED] Undocumented options of date command and other commands Mr. ameya sathe Linux - General 3 08-12-2009 07:23 AM
How can I append the date/time stamp to commands in history? abefroman Linux - Security 2 05-29-2008 12:21 PM
what is the correct syntax order for tar with --after-date DATE, --newer DAT farhan Linux - General 1 03-16-2007 09:43 AM
how to see typed commands with date? vaoc Linux - Security 1 08-06-2004 02:43 PM


All times are GMT -5. The time now is 04:53 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration