Solved

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

Posted on 2004-10-12
3
223 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

RIA (Rich Internet Application) tools are interactive internet applications which have many of the characteristics of desktop applications. The RIA tools typically deliver output either by the way of a site-specific browser or via browser plug-in. T…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

863 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

Need Help in Real-Time?

Connect with top rated Experts

27 Experts available now in Live!

Get 1:1 Help Now