LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 12-27-2009, 02:09 AM   #1
satish.res
LQ Newbie
 
Registered: Dec 2009
Posts: 2

Rep: Reputation: 0
Thumbs up date calculation


i have a text file with many columns and rows ,like msisdn date plan etc
i want to get another column by subtracting 7 days from existing column on each row. how can we achieve using linux commands

plz help thanks in advance
 
Old 12-27-2009, 02:36 AM   #2
bartonski
Member
 
Registered: Jul 2006
Location: Louisville, KY
Distribution: Fedora 12, Slackware, Debian, Ubuntu Karmic, FreeBSD 7.1
Posts: 443
Blog Entries: 1

Rep: Reputation: 47
A column can be pulled from data using the commands 'cut' or 'awk' depending on the format of the data.

You can subtract 7 days using the '--date' option of the 'date' command:

Code:
$ date --date="Sun Dec 20 03:17:58 EST 2009 - 7 days"
Sun Dec 13 03:17:58 EST 2009
Feel free to ask questions about 'cut', 'awk' or 'date' as needed.

Code:
date --help
gives a good summary of its options, of which there are many, some of which may be useful to you.
 
Old 12-27-2009, 03:00 AM   #3
satish.res
LQ Newbie
 
Registered: Dec 2009
Posts: 2

Original Poster
Rep: Reputation: 0
like this in a file have more than million rows, i want subtract with 7 days on each row and get the output
MSISDN date
322423423 24/12/2009
464566546 14/01/2008
453344343 15/05/2007
 
Old 12-27-2009, 05:31 AM   #4
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,503

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Quote:
Originally Posted by satish.res View Post
like this in a file have more than million rows, i want subtract with 7 days on each row and get the output
MSISDN date
322423423 24/12/2009
464566546 14/01/2008
453344343 15/05/2007
Have you tried some of the commands suggested by bartonski? If you try the date command, you will see that the format dd/mm/yyyy is not valid for option -d, --date since it's not a default. Hence you have to arrange items and pass a valid format to the date command.

Anyway, since you have more than one million of rows, the date command is not advised (I remember shell programming manuals discouraged the intensive usage of the date command, but maybe this is not valid in recent days). I suggest a little awk code like this
Code:
BEGIN { getline; printf "%-9s %-4s\n", $1, $2 }
{
  split($2, array, "/")
  datespec  = sprintf("%s %s %s 0 0 0",array[3],array[2],array[1])
  timestamp = mktime(datespec) - 7 * 86400
  print $1, strftime("%d/%m/%y", timestamp) 
}
You can try that on the very first lines of the file. I repeat invitation by bartonski. For any doubt related to awk, cut or any other command feel free to ask and please take in mind that the more you ask/explain the more you get a suitable help. Even better if you show us your attempts, codes, desired output.

Welcome to LinuxQuestions!
 
  


Reply

Tags
calculation, date


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 07:37 AM
shell script to find modified date and last accessed date of any file. parasdua Linux - Newbie 6 04-22-2008 09:59 AM
Setting system date and time affecting the clock and date on BIOS satimis Ubuntu 7 09-21-2007 08:02 AM
what is the correct syntax order for tar with --after-date DATE, --newer DAT farhan Linux - General 1 03-16-2007 08:43 AM


All times are GMT -5. The time now is 09:20 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration