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.
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.
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.
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...
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
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.