LinuxQuestions.org
Help answer threads with 0 replies.
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 03-11-2010, 03:55 PM   #1
Jeff1
LQ Newbie
 
Registered: Mar 2010
Posts: 3

Rep: Reputation: 0
Manipulating a Date in a perl bash script


Good afternoon. I've recently inherited a bunch of files at a new job and am trying to figure out some of the problems that have constantly popped up. The one i'm getting a huge headache with results from a bash script that is supposed to change a date format from a client populated txt field to one we want defined a certain way. Everything in the script works fine, except that one function.

Below is the line i'm trying to manipulate, with date examples.

sed -e 's/.0000\tEA/\tEA/' -e 's/\t01012010\t/\t01-JAN-2010\t/' -e 's/\t12312011\t/\t31-DEC-2011\t/'

The one caveat is that the first date is non-static and changes daily. It is, however, always the current date. If it helps, the second date will always be a year away from the first date.

My idea was to pull the current date via perl's DATE function, but...how to do it, and calculate a year away without throwing the rest of the bash script off?

Any help would be appreciated. I'm sure it's a simple solution but i know absolutely nothing about these scripts and how they were written. I tried finding a solution here already, but wasn't able to see anythign that described what i was looking for.

Thanks,

Jeff

Last edited by Jeff1; 03-11-2010 at 03:59 PM.
 
Old 03-11-2010, 04:04 PM   #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
Hi and welcome to LinuxQuestions!

Can you provide an example of the input date along with the desired transformation? Basically using the GNU date command, you can still do a lot of date calculations.
 
Old 03-11-2010, 04:22 PM   #3
Jeff1
LQ Newbie
 
Registered: Mar 2010
Posts: 3

Original Poster
Rep: Reputation: 0
colucix,

Sure.

The date we get in from our vendor comes across as (today's, for example): 03112010. No dashes, no other formatting.

The format were are trying to get to is:
11-MAR-2010

Same formatting for the second set of dates that appear in the code a year out from the current date.

Again, the date changes every day, to that current day, but always comes in that format.

(and i know people are probably asking us why we can't just make the vendor change it to the right format, right? easier said than done.)
 
Old 03-11-2010, 04:24 PM   #4
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
What exactly is a "perl bash script"?
 
Old 03-11-2010, 04:29 PM   #5
rweaver
Senior Member
 
Registered: Dec 2008
Location: Louisville, OH
Distribution: Debian, CentOS, Slackware, RHEL, Gentoo
Posts: 1,833

Rep: Reputation: 167Reputation: 167
Quote:
Originally Posted by Jeff1 View Post
Code:
sed -e 's/.0000\tEA/\tEA/' -e 's/\t01012010\t/\t01-JAN-2010\t/' -e 's/\t12312011\t/\t31-DEC-2011\t/'
So...

Code:
sed -e 's/.0000\tEA/\tEA/' -e "s/\t$(date +%m%d%Y)\t/\t$(date +%d-%b-%Y | tr [:lower:] [:upper:])\t/" -e "s/\t$(date --date=1year +%m%d%Y)\t/\t$(date --date=1year +%d-%b-%Y | tr [:lower:] [:upper:])\t/"
That should work in place of the existing sed line you have with the current date and the current date +1yr... you could probably do this cleaner in perl or clean it up and shorten it some, but this will do what you need it to do based on what you provided.

edit: the relevent bits are as follows--

date +%m%d%Y = date in MMDDYYYY format
date +%d-%b-%Y = date in DD-MON-YYYY format
date --date=1year = shift date by one year
tr [:lower:] [:upper:] = translate all lowercase into uppercase
$(whatever) = execute "whatever" in a shell

Rest is just sed pattern matching, this will consistently return the correct information each day its run for that date if the files you're getting are for yesterday for example then you could do a --date and take a day away, just remember to apply anything you do consistently. Keep in mind you need to pipe some kind of data to this or alternately use the sed against a file and do something with the output (or change it to inline mode.)

What the code above translates to is something like this...
Code:
-e "s/	03112010	/	11-MAR-2010	/" 
-e "s/	03112011	/	11-MAR-2011	/"
Each days the fields will increment to get to the current day and the current day+1year.

Last edited by rweaver; 03-11-2010 at 05:00 PM.
 
Old 03-11-2010, 04:30 PM   #6
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by Jeff1 View Post
...
the second date will always be a year away from the first date.
...
No, it won't - think of leap years having the 29th of February.
 
Old 03-11-2010, 04:49 PM   #7
rweaver
Senior Member
 
Registered: Dec 2008
Location: Louisville, OH
Distribution: Debian, CentOS, Slackware, RHEL, Gentoo
Posts: 1,833

Rep: Reputation: 167Reputation: 167
Quote:
Originally Posted by Sergei Steshenko View Post
No, it won't - think of leap years having the 29th of February.
That was one reason I used +1year with the date command instead of manual shifting, using date and shifting it a year it should at least give the correct data that won't fail although you will end up with 1 day on leap year that overlaps, that can't really be helped. The year before you will end up setting nothing for the 29th also so it really effects 2 years for every time leap year shows up. The next leap year is 2012. If you wanted an actual "365 days" you could do that instead of +1year also... which would account for leap year more accurate.

Code:
core:~$ date --date=02/28/2012+1year +%d%m%Y
28022013
core:~$ date --date=02/29/2012+1year +%d%m%Y
01032013
core:~$ date --date=03/01/2012+1year +%d%m%Y
01032013

core:~$ date --date=02/28/2012+365days +%d%m%Y
27022013
core:~$ date --date=02/29/2012+365days +%d%m%Y
28022013
core:~$ date --date=03/01/2012+365days +%d%m%Y
01032013

Edit: The two days effected will be: February 29th of leap year, it will never be set as an ending date. March 1st after leap year will have transactions for February 29th and March first. You can get a 'cleaner' transition by using +365days but it will throw your days off for any any year that is a leap year for part of the year

Last edited by rweaver; 03-11-2010 at 05:07 PM.
 
Old 03-12-2010, 08:57 AM   #8
Jeff1
LQ Newbie
 
Registered: Mar 2010
Posts: 3

Original Poster
Rep: Reputation: 0
Thanks to everyone for the help.
Everything makes total sense, and it looks like it's doing what i need it to do.

I appreciate all the explanation here, i'm going to be hanging around a bit as a learn what i'm doing here.

Hopefully i'll be getting some official training and get a real understanding of the programming instead of trying to go trial and error on these existing scripts.

Thanks again!
 
Old 03-12-2010, 09:32 AM   #9
rweaver
Senior Member
 
Registered: Dec 2008
Location: Louisville, OH
Distribution: Debian, CentOS, Slackware, RHEL, Gentoo
Posts: 1,833

Rep: Reputation: 167Reputation: 167
If i were going to recommend books for learning bash these would be at the top of my list:

Bash Quick Reference
Bash Cookbook
Learning the bash Shell, Third Edition

The third and first of course more useful than the middle item, but it definitely has its place.
 
Old 03-15-2010, 04:43 PM   #10
rweaver
Senior Member
 
Registered: Dec 2008
Location: Louisville, OH
Distribution: Debian, CentOS, Slackware, RHEL, Gentoo
Posts: 1,833

Rep: Reputation: 167Reputation: 167
O'reilly publishing has some excellent perl books too. I own the learning, base, and cookbook and use them all when I'm actively doing perl if you're interested in learning that better.
 
Old 03-15-2010, 08:54 PM   #11
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,358

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Some bash manuals/howtos:
http://rute.2038bug.com/index.html.gz
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/

Perl:
http://perldoc.perl.org/
http://www.perlmonks.org/?node=Tutorials

and Welcome to LQ
 
  


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
Batch manipulating CSV columns and files in Perl script briana.paige Linux - Newbie 1 07-14-2009 11:02 AM
Perl/bash - calculate date in future rose_bud4201 Programming 6 05-10-2009 05:59 AM
bash script on manipulating files fiomba Linux - Software 8 10-30-2004 08:31 PM
Date calculations in BASH script Crashman Programming 4 07-03-2004 10:15 AM
Date in a BASH script... soulsniper Linux - Software 7 11-22-2003 06:08 PM

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

All times are GMT -5. The time now is 08:01 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