Connecting Tech Pros Worldwide Help | Site Map

String manipulation for a date

  #1  
Old June 23rd, 2009, 09:19 PM
Member
 
Join Date: Sep 2008
Posts: 35
A file contains the following contents ...

Expand|Select|Wrap|Line Numbers
  1. 20090224T105933 s990atap01      Statistics      0       curam.util.internal.workflow.intf.WorkflowQueueHandler.handleWorkflow
  2. EnactmentMessage ( java.lang.String java.lang.String java.lang.Long )   true    5608
  3. 20090224T105940 s990atap01      Statistics      0       curam.util.internal.workflow.intf.WorkflowActivityQueueHandler.handle
  4. ActivityMessage ( java.lang.String java.lang.Long java.lang.Long java.lang.Long java.lang.String )      true    7308
  5.  
I want to replace the date/time string with the following....

2009-02-24-10.59.33.000000 ......
2009-02-24-10.59.40.000000 .....

Of course, this has to repeat for the complete file.

Is there a Perl command that would do it?
  #2  
Old June 24th, 2009, 02:52 AM
KevinADC's Avatar
Expert
 
Join Date: Jan 2007
Location: Southern California USA
Posts: 4,097

re: String manipulation for a date


There is no perl command to do that but you could use a regexp to capture the date part of the string/line (or maybe another way depending on how the actual data is) and use unpack (or a regexp) and sprintf to reformat it like you want.
  #3  
Old June 24th, 2009, 05:45 PM
Member
 
Join Date: Sep 2008
Posts: 35

re: String manipulation for a date


What would the regular expression look like?
  #4  
Old June 24th, 2009, 09:21 PM
KevinADC's Avatar
Expert
 
Join Date: Jan 2007
Location: Southern California USA
Posts: 4,097

re: String manipulation for a date


Quote:
Originally Posted by joeferns79 View Post
What would the regular expression look like?
probably something like:

Expand|Select|Wrap|Line Numbers
  1. /searchpattern/
  #5  
Old June 25th, 2009, 02:59 PM
Member
 
Join Date: Sep 2008
Posts: 35

re: String manipulation for a date


Man, you truly are an Expert, aren't you? :-)
  #6  
Old June 25th, 2009, 05:22 PM
KevinADC's Avatar
Expert
 
Join Date: Jan 2007
Location: Southern California USA
Posts: 4,097

re: String manipulation for a date


My wife always says I am. My kids are generally less complimentary though.
  #7  
Old August 21st, 2009, 09:32 PM
Member
 
Join Date: Sep 2008
Posts: 35

re: String manipulation for a date


Kevin,

I got the following code for this ...

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. $mystring = "20090216T131131";
  4. print "The input is $mystring";
  5. printf "\n";
  6. @myarray = ($mystring =~ m/(\d+)/g);
  7. $result = join("", @myarray);
  8. my ($y,$m,$d,$h,$min,$sec) = unpack("A4A2A2Z2Z2Z2",$result);
  9. $done = sprintf ("%d-%2.2d-%d-%d.%d.%d.%06s",$y,$m,$d,$h,$min,$sec,0);
  10. print "$done";
  11. printf "\n";
  12.  
which returns the following ...

Expand|Select|Wrap|Line Numbers
  1. bash-3.00$ ./test.pl
  2.  
  3. The input is 20090216T131131
  4. 2009-02-16-13.11.31.000000
  5.  
  6.  
The problem is i've got a file that contains a number of occurrences of the timestamp, eg
Expand|Select|Wrap|Line Numbers
  1. 20090224T105710 fem.facade.intf.caseSearch
  2. 20090224T105710 codetable.intf.CodeTable
  3. 20090224T105719 intf.FieldLevelSecurityRuntime
  4. 20090224T105723 resolveCase
  5. 20090224T105724 FieldLevelSecurity
  6.  
If I need to use the code above to change all the occurences of a timestamp, one at a time, to the format specified, and feed those back into the file, how should I go about it?

i.e. I want to change 20090224T105710 to 2009-02-24-10.57.10.000000,
20090224T105719 to 2009-02-24-10.57.19.000000, and so on.

Last edited by numberwhun; August 22nd, 2009 at 02:05 PM. Reason: Please use square brackets for code tags, not angle brackets
  #8  
Old August 22nd, 2009, 06:14 AM
KevinADC's Avatar
Expert
 
Join Date: Jan 2007
Location: Southern California USA
Posts: 4,097

re: String manipulation for a date


Expand|Select|Wrap|Line Numbers
  1. open(my $data, "<", "path/to/your/file") or die "$!"; 
  2. while(my $mystring = <$data>){
  3.    print "The input is $mystring";
  4.    my ($date, $otherstuff) = split(/\s+/,$mystring);
  5.    my ($y,$m,$d,$h,$min,$sec) = unpack("A4A2A2Z2Z2Z2",$date);
  6.    my $done = sprintf ("%d-%2.2d-%d-%d.%d.%d.%06s",$y,$m,$d,$h,$min,$sec,0);
  7.    print "$done\n";
  8. }
  9. close $data;
  10.  
  #9  
Old August 22nd, 2009, 02:06 PM
numberwhun's Avatar
Site Moderator
 
Join Date: May 2007
Location: New Hampshire
Posts: 2,543

re: String manipulation for a date


Please use square brackets for code tags instead of angle brackets and you will get the desired effect that we require. Thanks!

Regards,

Jeff
  #10  
Old August 24th, 2009, 04:29 PM
Member
 
Join Date: Sep 2008
Posts: 35

re: String manipulation for a date


Kevin,

After changing my code I now get only the dates ...

2009-02-24-10.57.10.000000
2009-02-24-10.57.10.000000
2009-02-24-10.57.19.000000
2009-02-24-10.57.23.000000
2009-02-24-10.57.24.000000
2009-02-24-10.57.50.000000
2009-02-24-10.57.50.000000
2009-02-24-10.57.50.000000
2009-02-24-10.57.50.000000
2009-02-24-10.57.50.000000
2009-02-24-10.51.8.000000
2009-02-24-10.51.8.000000
2009-02-24-10.51.8.000000
2009-02-24-10.51.16.000000
2009-02-24-10.54.4.000000
2009-02-24-10.54.4.000000

I do not get the rest of the line, i.e.

I should be getting ...

2009-02-24-10.57.10.000000 fem.facade.intf.caseSearch
2009-02-24-10.57.10.000000 codetable.intf.CodeTable
2009-02-24-10.57.19.000000 intf.FieldLevelSecurityRuntime
2009-02-24-10.57.23.000000 resolveCase

.. so on
  #11  
Old August 24th, 2009, 05:54 PM
KevinADC's Avatar
Expert
 
Join Date: Jan 2007
Location: Southern California USA
Posts: 4,097

re: String manipulation for a date


change:

Expand|Select|Wrap|Line Numbers
  1.  print "$done\n";
to:

Expand|Select|Wrap|Line Numbers
  1.    print "$done $otherstuff\n";
  #12  
Old August 24th, 2009, 08:08 PM
Member
 
Join Date: Sep 2008
Posts: 35

re: String manipulation for a date


Thanks, Kevin. Appreciate it.
  #13  
Old August 25th, 2009, 06:46 PM
Newbie
 
Join Date: Aug 2009
Location: Los Angeles, CA
Posts: 2

re: String manipulation for a date


IMHO, unpack seems like a lot of extra work.

Try this one-liner using regular expression:

Expand|Select|Wrap|Line Numbers
  1. $ perl -pi.bak -e 's/(....)(..)(..)T(..)(..)(..)/$1-$2-$3-$4.$5.$6.000000/' /tmp/junk.txt
  2.  

Last edited by numberwhun; August 27th, 2009 at 03:02 AM. Reason: Please use code tags!
  #14  
Old August 27th, 2009, 03:01 AM
numberwhun's Avatar
Site Moderator
 
Join Date: May 2007
Location: New Hampshire
Posts: 2,543

re: String manipulation for a date


Quote:
Originally Posted by markhu View Post
IMHO, unpack seems like a lot of extra work.

Try this one-liner using regular expression:

Expand|Select|Wrap|Line Numbers
  1. $ perl -pi.bak -e 's/(....)(..)(..)T(..)(..)(..)/$1-$2-$3-$4.$5.$6.000000/' /tmp/junk.txt
And if your going to do that, take it one step further and do:

Expand|Select|Wrap|Line Numbers
  1. $ perl -pi.bak -e 's/(\d{,4})(\d{,2})(\d{2})T(\d{,2})(\d{,2})(\d{,2})/$1-$2-$3-$4.$5.$6.000000/' /tmp/junk.txt
  2.  
A bit more elegant, but also untested at the moment, but should work none the less (unless I, without looking, messed up the options in the curly brackets).

By the way, please do use code tags around any code you put into the forums. Even command likes as you have put here as they essentially have code in them.

Regards,

Jeff
  #15  
Old August 27th, 2009, 10:29 AM
nithinpes's Avatar
Expert
 
Join Date: Dec 2007
Posts: 400
Provided Answers: 1

re: String manipulation for a date


Jeff's command should have been:

Expand|Select|Wrap|Line Numbers
  1. perl -pi.bak -e 's/(\d{4})(\d{2})(\d{2})T(\d{2})(\d{2})(\d{2})/$1-$2-$3-$4.$5.$6.000000/' /tmp/junk.txt
  2.  
\d{,4} will set an upper limit of 4 occurences for digits(max. 4 digits) and not exact 4 occurences as in \d{4} (which is appropriate for the given sample data).
  #16  
Old August 27th, 2009, 02:43 PM
numberwhun's Avatar
Site Moderator
 
Join Date: May 2007
Location: New Hampshire
Posts: 2,543

re: String manipulation for a date


Quote:
Originally Posted by nithinpes View Post
Jeff's command should have been:

Expand|Select|Wrap|Line Numbers
  1. perl -pi.bak -e 's/(\d{4})(\d{2})(\d{2})T(\d{2})(\d{2})(\d{2})/$1-$2-$3-$4.$5.$6.000000/' /tmp/junk.txt
  2.  
\d{,4} will set an upper limit of 4 occurences for digits(max. 4 digits) and not exact 4 occurences as in \d{4} (which is appropriate for the given sample data).
See, that's why I included the "if". I couldn't remember right off if the comma should be there or not and couldn't get up the energy to dig for my reference sheet. Thanks!

Jeff
Reply


Similar Threads
Thread Thread Starter Forum Replies Last Post
Classic ASP String Manipulation - NOT .net James answers 6 February 16th, 2006 01:25 PM
Converting VARCHAR "date" info to an actual date field usenet@isotopeREEMOOVEmedia.com answers 3 August 16th, 2005 08:35 PM
String manipulation / TEXTAREA input help yerk5@hotmail.com answers 8 July 23rd, 2005 07:24 PM
Convert User entered date and time to Unix Timestamp before inserting to database perplexed answers 13 July 17th, 2005 11:00 AM