date conversion


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting date conversion
# 8  
Old 03-31-2008
hi

Quote:
Originally Posted by era
Take out the Useless Use of Cat while you are at it.

I don't think basic sed knows about $1 $2 $3, try with \3\2\1 instead.

Code:
sed -e 's/([0-9][0-9])-([[:alpha:]]+)-([0-9]{4})/\3\2\1/' file

But then, if you have a really pedestrian sed, it probably doesn't grok [[:alpha:]] or {4} either.

Code:
sed -e 's/([0-9][0-9])-([A-Za-z]+)-([0-9][0-9][0-9][0-9])/\3\2\1/' file


I getting this error


$ sed -e 's/([0-9][0-9])-([[:alpha:]]+)-([0-9]{4})/\3\2\1/' file6
sed: -e expression #1, char 48: invalid reference \3 on `s' command's RHS

$ sed -e 's/([0-9][0-9])-([A-Za-z]+)-([0-9][0-9][0-9][0-9])/\3\2\1/' file6
sed: -e expression #1, char 57: invalid reference \3 on `s' command's RHS

Please let me know about this code in detail

Thanks for your response
# 9  
Old 03-31-2008
hi

Quote:
Originally Posted by fpmurphy
If you have ksh93 version h or better you can use the printf %T feature to easily do the date format conversion i.e.

Code:
#!/usr/bin/ksh93

IFS=','
while read v1 v2 v3 v4
do
    printf "%s,%s,%(%Y%m%d)T,%s\n" $v1 $v2 $v3 "$v4"
done < file

Hi i am getting this error

file7: line 4: printf: `(': invalid format character

Please let me know on this asap........

Thanks for your response
charan
# 10  
Old 03-31-2008
Stupid of me to miss that. Most sed implementations want backslashes on the parentheses.

Code:
sed -e 's/\([0-9][0-9]\)-\([A-Za-z]+\)-\([0-9][0-9][0-9][0-9]\)/\3\2\1/' file

And if they're really stupid they don't even understand the +

Code:
sed -e 's/\([0-9][0-9]\)-\([A-Za-z][A-Za-z]*\)-\([0-9][0-9][0-9][0-9]\)/\3\2\1/' file

But that just rearranges the order of the fields, it doesn't magically convert month names to numbers. There was another thread on that recently.

The printf format error would seem to indicate you do not have ksh93 version h or better, is that correct? Read what fpmurphy wrote.

The general problem of a myriad different date formats is still not really tackled. I don't know if it could be -- how do you know if 03/12 is December 3rd or March 12th? (In computer-generated dates, dashes are usually an indication of European, aka sane order, whereas slashes tend to be used in American dates; but it depends on the application which generated these. Or are they human-generated? And of course, if they are written by Americans, you can make certain assumptions.)

Last edited by era; 03-31-2008 at 12:51 AM..
# 11  
Old 03-31-2008
Quote:
i am getting this error

file7: line 4: printf: `(': invalid format character

Please let me know on this asap........
Then you are not using ksh93 verision h or later! echo ${.sh.version} to see ksh93 version string.
# 12  
Old 03-31-2008
Just to top off this thread, here's a feeble attempt at converting month names to numbers, in sed.

Code:
# hack: start at 100 and drop the first digit to easily get leading zeros where required
c=100
for m in '[Jj]an' '[Ff]eb' '[Mm]ar' '[Aa]pr' '[Mm]ay' '[Jj]un' \
         '[Jj]ul' '[Aa]ug' '[Ss]ep' '[Oo]ct' '[Nn]ov' '[Dd]ec'
do
  c=`expr $c + 1`
  cat <<__HERE
    s/\\([0-9][0-9]\\)-$m-\([0-9][0-9][0-9][0-9]\)/\2${c#1}\1/
__HERE
done | sed -f - file.txt

# 13  
Old 04-01-2008
hi

Quote:
Originally Posted by era
Just to top off this thread, here's a feeble attempt at converting month names to numbers, in sed.

Code:
# hack: start at 100 and drop the first digit to easily get leading zeros where required
c=100
for m in '[Jj]an' '[Ff]eb' '[Mm]ar' '[Aa]pr' '[Mm]ay' '[Jj]un' \
         '[Jj]ul' '[Aa]ug' '[Ss]ep' '[Oo]ct' '[Nn]ov' '[Dd]ec'
do
  c=`expr $c + 1`
  cat <<__HERE
    s/\\([0-9][0-9]\\)-$m-\([0-9][0-9][0-9][0-9]\)/\2${c#1}\1/
__HERE
done | sed -f - file.txt



it is not working.......
file1
E108,0,2/3/1995,0,E001,E003,A,15000,1250,7.211538
E109,0,2/15/1995,0,E001,E001,A,78000,6500,37.5
E110,0,10/12/1995,0,E001,E001,A,56000,4666.667,26.923077

I wann to convert the this file1 to file2

E108,0,199523,0,E001,E003,A,15000,1250,7.211538
E109,0,1995215,0,E001,E001,A,78000,6500,37.5
E110,0,19951012,0,E001,E001,A,56000,4666.667,26.923077


and on this file i am going to do more process like sort and compare.....with other files which has this date format...........


And is it possible to hold the date value in a string variable while reading from a file..........i am here with developing a tool which should process within a min......And i am new to unix.


Please let me know on this how to do that..........asap
# 14  
Old 04-03-2008
The code I posted only handles the conversion of abbreviated month names to numbers.

I don't understand "hold the date value in a string variable while reading from a file", I mean yes you can do that, but do you mean read it for each line, or what? Backticks would be a typical solution to this sort of problem, or if you are reading the file line by line in a while loop, then by all means continue to do that.

I would write a small awk or perl script with heuristics for each possible date format. Only you know how wild the variation is; I have seen three different formats in the samples you have posted, so tackling only those should be doable.

Code:
perl -naF, -e 'BEGIN { my @m = qw(dummy jan feb mar apr
    may jun jul aug sep nov dec);
  %m = map { $m[$_] => $_ } 1..12;
  $m = join ("|", keys %m);
}
if ($F[2] =~ m%(\d{1,2})/(\d{1,2})/(\d{4})%) {
  $F[2] = sprintf "%04i%02i%02i", $3, $1, $2;
} elsif ($F[2] =~ m%(\d{1,2})-($m)-(\d{4})%i) {
  $F[2] = sprintf "%04i%02i%02i", $3, $m{$2}, $1;
} elsif ($F[2] =~ m%(\d{1,2})-($m)-(\d{2})%i) {
  $F[2] = sprintf "%04i%02i%02i", 2000+$3, $m{$2}, $1;
}
print join (",", @F)'

This is untested
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Date conversion

Hi , we have a string with yyyymmdd format . how to know which date it is ? example:20120712-->sunday 20150228-->saturday 20140431-->invalid please suggest commands which work on below os : SunOS 5.10 shell: bash shell Thanks, Srinath. (10 Replies)
Discussion started by: srinadhreddy27
10 Replies

2. Shell Programming and Scripting

Date conversion

Trying to convert dates using a Perl Script but it has to accept formats like 3 letter month, day and year like Nov 02 2010 or 1/4/11 or 21 Feb 2011 and have it convert to something like October 20, 2011. Any ideas? (2 Replies)
Discussion started by: reduxeffect81
2 Replies

3. Shell Programming and Scripting

Julian date to Calendar date conversion

Hi all, I require to convert julian date to normal calander date in unix for eg julian date=122 now i want corresponding calander date ---------------------------------------- gr8 if give very small command/script and please explain the steps as well(imp) Thanks ... (3 Replies)
Discussion started by: RahulJoshi
3 Replies

4. UNIX for Dummies Questions & Answers

Date conversion in ab i

(string(8)) ((date("YYYYMMDD")) ((date("YYYY/MM/DD")) in.date_field_name)) (1 Reply)
Discussion started by: dr46014
1 Replies

5. Shell Programming and Scripting

Conversion of date to Julian date

Hi Gurus, Need help in Conversion of date(2007-11-30) to Julian date(YYDDD)... '+%J' 2007-11-30 to 'YYDDD' Thanks (4 Replies)
Discussion started by: SeenuGuddu
4 Replies

6. Shell Programming and Scripting

Date conversion

Hi, I have the string YYYYMMDDHHMMSS like 20090801204150 and I need to convert it using the unix command date in the format: date "Saturday, 1 August 2009 20:40:59" All in one single Unix line if this is possible. What's the correct syntax? Steve Hagi (6 Replies)
Discussion started by: hagimeno
6 Replies

7. Shell Programming and Scripting

Date conversion

Hi I want to convert MAY 05 2005 01:15:00PM date format to 2005/05/05 01:15:00PM . CAn somebody suggest me a code ,I am new to unix shell programming. Thanks Arif (21 Replies)
Discussion started by: mab_arif16
21 Replies

8. Shell Programming and Scripting

Date Conversion

Hi, Does anyone know (in KSH, CSH, SED or AWK), how to convert date text in a file from: EX: May232008 to: 05232008 Thanks, (3 Replies)
Discussion started by: jgrant746
3 Replies

9. Shell Programming and Scripting

date conversion

Hi everybody: Could anybody tell me how I convert from a julian date, with shell comands, to gregorian. Thanks in advance. (2 Replies)
Discussion started by: tonet
2 Replies

10. UNIX for Advanced & Expert Users

Date Conversion

Hello, I want to convert MM DD YYYY date format to MM-DD-YYYY format. For exemple: I have to convert Nov 28 2005 to 28-11-2005. Thenks for youf help. DAFI (2 Replies)
Discussion started by: dafidak
2 Replies
Login or Register to Ask a Question