Solved

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

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
dividesSelf challange 15 79
How to split this in C++ 4 77
Microsoft C++ code failing in executable that worked 9 54
Not needed 13 57
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…
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
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…
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…

757 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

21 Experts available now in Live!

Get 1:1 Help Now