Solved

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

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

I know it’s not a new topic to discuss and it has lots of online contents already available over the net. But Then I thought it would be useful to this site’s visitors and can have online repository on vim most commonly used commands. This post h…
This article will show, step by step, how to integrate R code into a R Sweave document
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…

831 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