Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2004-10-12
3
Medium Priority
?
231 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 750 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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

Make the most of your online learning experience.
If you are a mobile app developer and especially develop hybrid mobile apps then these 4 mistakes you must avoid for hybrid app development to be the more genuine app developer.
Simple Linear Regression
Progress

916 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