LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   date calculation (https://www.linuxquestions.org/questions/linux-newbie-8/date-calculation-778273/)

satish.res 12-27-2009 02:09 AM

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

bartonski 12-27-2009 02:36 AM

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.

satish.res 12-27-2009 03:00 AM

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

colucix 12-27-2009 05:31 AM

Quote:

Originally Posted by satish.res (Post 3805703)
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! :)


All times are GMT -5. The time now is 04:12 PM.