working with date


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting working with date
# 1  
Old 03-02-2007
working with date

I wan to convert any field having date in the file from the follwoing format 5/1/2003 to 2003-05-01. This file is tab delimited. Please help.

Last edited by dsravan; 03-02-2007 at 06:51 PM.. Reason: typo
# 2  
Old 03-02-2007
Here is a sed(1) script file that should solve your problem. The ^I is just the [Tab] key but it's easier to see.
Code:
s;^\([0-9]\{1,2\}\)/\([0-9]\{1,2\}\)/\([0-9]\{4\}\)^I;\3-^A0\1-^A0\2^I;
s;^I\([0-9]\{1,2\}\)/\([0-9]\{1,2\}\)/\([0-9]\{4\}\)$;^I\3-^A0\1-^A0\2;
s;^I\([0-9]\{1,2\}\)/\([0-9]\{1,2\}\)/\([0-9]\{4\}\)^I;^I\3-^A0\1-^A0\2^I;g
s;^A0\([0-9][0-9]\);\1;g
s;^A;;g

The "^A" is a CTRL-A character NOT a circumflex followed by an uppercase A. It is used instead of, say, a '+' to avoid possible matches in the data. Here is what the script file does:

1 ) Special case where the date string is the first field in the row and is not preceeded by a tab character,
2 ) Special case where the date string is the last field in the row and is not followed by a tab character,
3 ) The general case where you have a tab, date string, and a tab. It is applied to all of the date fields not covered by the two special cases,
4 ) Replace all occurences of ^A0## which would occur if either the day of month or month in year substring was already two digits,
5 ) Get rid of any stray ^A characters left over when the day of month or month in year substring was only one digit

It's confusing but it works. Let me know if you have any questions.

Last edited by hegemaro; 03-02-2007 at 08:17 PM.. Reason: Replace $TAB with ^I or tab. I goofed the order of day and month too.
# 3  
Old 03-02-2007
Quote:
Originally Posted by dsravan
I wan to convert any field having date in the file from the follwoing format 5/1/2003 to 2003-05-01. This file is tab delimited. Please help.
Code:
awk '
  function convdate ( date ) {
   split( date,a,"/" )
   ## Assuming M/D/YYYY; otherwise, swap a[1] and a[2]
   return sprintf( "%4d-%02d-%02d", a[3], a[1], a[2] )
  }
  BEGIN { IFS = OFS = "\t" }
     /[0-9]+\/[0-9]+\/[0-9][0-9][0-9][0-9]/ {
       n = 0
       while ( ++n <= NF ) 
           if ( $n ~ /[0-9]+\/[0-9]+\/[0-9][0-9][0-9][0-9]/ ) $n = convdate($n)
   }
   { print }'

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script to get last working date

Problem : we need a command that will give previous day date and previous day should be mon-fri only. Example :if we are running a command on Monday , it will give Friday ‘s date not Sunday’s . Few more restrictions : Command should be working in NM n/w. Hard coding of date should be avoided.... (11 Replies)
Discussion started by: Downsouth
11 Replies

2. UNIX for Dummies Questions & Answers

Date Validation not working

Hi Experts, I have a date validation script in that i will validate the date for a given format and search in the logs for that date. The script logic is very simple like below. Validate_Date() { is_valid=1 while do date_format=$(date "+$1") echo -e "Please enter the $2 date like... (6 Replies)
Discussion started by: senthil.ak
6 Replies

3. Shell Programming and Scripting

Date command is not working properly

Hi, in my script, i take the last month by a=$(date --date '1 month ago' +%Y%m) i expect that it give me in this month "March" as result 201402, but linux gave me 201403. IMPe@ABC123:> ~/date --date '1 month ago' +%Y%m 201403 i'm reasonably confused. Any idea? Thanks in advance, ... (2 Replies)
Discussion started by: IMPe
2 Replies

4. Shell Programming and Scripting

Date Format not working in python script

Good Morning, I am working on a pyhton script, where it has to fetch a file from our vendor's server over an ftp. The file is having a date format of "MMDDYYYY" (i have included in my python script), but when the script is executed it is unable to fetch the file. At the same time, if i... (1 Reply)
Discussion started by: AReddy
1 Replies

5. Shell Programming and Scripting

Converting a date to friday date and finding Min/Max date

Dear all, I have 2 questions. I have a file with many rows which has date of the format YYYYMMDD. 1. I need to change the date to that weeks friday date(Ex: 20120716(monday) to 20120720). Satuday/Sunday has to be changed to next week friday date too. 2. After converting the date to... (10 Replies)
Discussion started by: 2001.arun
10 Replies

6. Shell Programming and Scripting

[Solved] Working with date (add minutes using variables)

Dear all, today I'm scratching my head with a simple (I believe) issue. Working with date is quite simple, so if I Need to add some seconds to current time, I'll use: date --date='+30 seconds' +"%Y-%m-%d %H:%M:%S"But, how to pass the value to add from a variable? I tried the following without... (2 Replies)
Discussion started by: Lord Spectre
2 Replies

7. Shell Programming and Scripting

date command not working

hi #!usr/bin/perl -w local ($date) = `/sbin/date "+%D %X" ` ; print $date when i run this in ksh shell it is giving the below error sh: /sbin/date: not found but same code is working and displaying date and time in sh shell. what could be the reason. pls help (10 Replies)
Discussion started by: psthariharan
10 Replies

8. Shell Programming and Scripting

cp -p /home/* home/exp/*.date not working please help

:( ---------- Post updated at 01:51 AM ---------- Previous update was at 01:50 AM ---------- Not working ---------- Post updated at 02:04 AM ---------- Previous update was at 01:51 AM ---------- cp -p /home/* home/exp/*.`date` i am using this (4 Replies)
Discussion started by: rishiraaz
4 Replies

9. Shell Programming and Scripting

Working with bash and date

Hello all, I'm trying to substract 1 minute from the current date and take the hour and minute (for filename purpose). 1) If I want hour an minute from current time I can use: timetmp=$(date +"%H:%M") 2) To substract 1 minute from current time I can use: timetmp=$(date --date "$dte -1... (8 Replies)
Discussion started by: Lord Spectre
8 Replies

10. UNIX for Dummies Questions & Answers

Date command to obtain the last month is not working correctly..

Hello, I could not find the exactly same post here.. so I will explain what I did to get the last month using date command. I used date +%Y-%m -d "-1 months" to get the last month. However, the returned value of above command on 2009/10/31 was 2009 10 and not 2009 09.. and the... (9 Replies)
Discussion started by: tigersk
9 Replies
Login or Register to Ask a Question