Solved

How can we extract the line that has the greatest or MAX timeDateStamp from a text file?

Posted on 2004-10-12
3
225 Views
Last Modified: 2010-04-17
Basically here is my situation:

We have a text file that contains the following data -

       timeStamp           tree    lck  p  code ...
2004-08-23 09:32:49,53866,3234,1,922,3,79,0,18,3,-31
2004-08-19 10:34:01,53866,3234,2,922,3,88,0,18,3,-22
2004-09-01 15:01:49,53866,3234,3,922,3,95,0,18,3,-15
2004-09-13 01:55:19,53866,3234,4,922,1,100,0,18,1,-10
2004-09-14 14:00:49,53866,3234,5,922,1,105,0,18,0,-5
2004-09-30 09:32:49,53866,3234,6,922,110,18,0,,,
2004-10-19 19:39:58,53866,3234,4,922,110,18,0,,,

Is there a way to extract the line that has the greatest timeStamp where p = 4?
0
Comment
Question by:mebjen
  • 2
3 Comments
 
LVL 20

Expert Comment

by:dsacker
ID: 12287949
Depending on what language you want to use, there are numerous ways:

1.  You could import that into an Excel spreadsheet. It looks comma-delimited, right up its alley.
2.  You could load it into a database (Oracle, Access, etc), and select the appropriate row based on your criteria.
3.  You could write a VBScript, VB, Unix script, etc (depending on your OS) and process the records via code.

What's your preference?
0
 

Author Comment

by:mebjen
ID: 12288015
dsacker,

my preference would be (3) doing it programmically using VB .NET, but I'm not even sure how to go about it.

0
 
LVL 20

Accepted Solution

by:
dsacker earned 250 total points
ID: 12289909
Here is a VBScript that will do it. You could use much of this in a .NET program:

Dim intCnt
Dim maxCnt
Dim objFileSystemObject
Dim objTextFile
Dim p
Dim datDate
dim maxDate
Dim strDate
Dim strRecord

Set objFileSystemObject = CreateObject("Scripting.FileSystemObject")
set objTextFile = objFileSystemObject.OpenTextFile("file.txt",1,2)

intCnt = 0
maxDate = CDate("01/01/1900")

Do While objTextFile.AtEndOfStream = False
   strRecord = objTextFile.ReadLine
   intCnt = intCnt + 1
   strDate = Left(strRecord, 19)
   p = Mid(strRecord, 32, 1)

   datDate = CDate(strDate)
   If p = 4 then
      If datDate > maxDate Then
         maxDate = datDate
         maxCnt  = intCnt
      End If
   End If
Loop

objTextFile.Close
WScript.Echo "Maximum date for p = 4 was " & maxDate & " (record #" & maxCnt & ")"

Set objTextFile = Nothing
Set objFileSystemObject = Nothing
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
A short article about problems I had with the new location API and permissions in Marshmallow
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question