Jump to content

Obtaining a text line 3 lines down


 Share

Recommended Posts

Hi all,

I'm sure this is easy but i can't seem to find an answer.

I need to get a text line 3 lines down from an ID#

example:

ID: xxxxx1 - misc useless data here

1-useless line info

2-useless line info

3-LINE I'm trying to get

4-useless lines and more follow

ID: xxxxx2 --(everything repeats......)

Now I've already been able to Get the ID:#'s and remove the useless data following it (in the same line) and place them in a text file. But i would like to get the 3rd line and add it to it's own ID# line.

i.e

ID: xxxxx1, 3-LINE I'm trying to get

So i can find the ID# but i can't seem to figure how to count lines down from it.

Hope i explained this right.

Thx Much

A.B.Ames

Link to comment
Share on other sites

Am I in the wrong goup? Is autoit v3 forum for those that already know how to script, Is there another group somewhere for people wanting and trying to learn?

I don't now how to read the next 2 lines and am trying to figure out how to read lines after the specific line I searched for.

I know what the @LF,@CR, & @ CRLF do but how do u use them to count down lines from a reference point?

Link to comment
Share on other sites

  • Developers

Am I in the wrong goup? Is autoit v3 forum for those that already know how to script, Is there another group somewhere for people wanting and trying to learn?

<{POST_SNAPBACK}>

Nope, it is in general assumed that pll try to do some reading first in the helpfile and have some basic knowledge.

I don't now how to read the next 2 lines and am trying to figure out how to read lines after the specific line I searched for.

I know what the @LF,@CR, & @ CRLF do but how do u use them to count down lines from a reference point?

<{POST_SNAPBACK}>

read about the FileReadLine() command in the helpfile and it example. it shows how you can read a file line by line..... :idiot: Edited by JdeB

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

I did read it and it tells me how to read a Line number but the lines i'm trying to read are at random line numbers. That's why I'm here asking.

from manual:

FileReadLine ( filehandle or "filename" [, line] )

line [optional] The line number to read. The first line of a text file is line 1 (not zero).

I'm using FileReadLine to read the file and then using StringInStr to locate the ID: line and write them to text but i'm confused as to how to count down lines after finding ID:

Link to comment
Share on other sites

  • Developers

Not tested, but should give some ideas:

$file = FileOpen("test.txt", 0)
; Check if file opened for reading OK
If $file = -1 Then
   MsgBox(0, "Error", "Unable to open file.")
   Exit
EndIf
$count = 0     ; record counter
$ID_Found = 0; indicator for ID: found
; Read in lines of text until the EOF is reached
While 1
   $line = FileReadLine($file)
   If @error = -1 Then ExitLoop
   If $ID_Found = 1 Then $count = $count + 1
   If $count = 3 Then
      ExitLoop   ;
   EndIf
   If StringLeft($line, 3) = "ID:" Then $ID_Found = 1
   MsgBox(0, "Line read:", $line)
Wend
If $ID_Found = 1 Then
   MsgBox(0, 'THIRD LINE', $line)
Else
   MsgBox(0, 'Error THIRD LINE', 'ID: not found.')
EndIf
FileClose($file)

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

thank you very much,

this helps me understand it a lot more.

Question though,

you used StringLeft to locate ID: where as I was using StringInStr

i.e.

If StringInStr($line,"ID:") Then $ID_Found = 1

vs

If StringLeft($line, 3) = "ID:" Then $ID_Found = 1

(both are you code I just tested with StringInStr to see if it worked too)

is it better or is it just a preference?

Again thx for helping,

A.B.Ames

Link to comment
Share on other sites

  • Developers

thank you very much,

this helps me understand it a lot more.

Question though,

you used StringLeft to locate ID: where as I was using StringInStr

i.e.

If StringInStr($line,"ID:") Then $ID_Found = 1 

vs

If StringLeft($line, 3) = "ID:" Then $ID_Found = 1

(both are you code I just tested with StringInStr to see if it worked too)

is it better or is it just a preference?

Again thx for helping,

A.B.Ames

<{POST_SNAPBACK}>

I tend to use StringLeft when testing for the left 3 characters just to avoid StringInstr matching to another record...

But there is nothing wrong with it when you are sure its unique for that one record.. :idiot:

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Hi all,

This works for what i need, I am just looking for opinion as to if I done this right so i can be sure of my understanding of it (and if i missed something I should have done).

Using what "JdeB" showed me I changed it as needed and to find all instances.

It Locates all "ID:" 's and the 3rd line down from each too, removes un-needed and joins the 2. msgbox if there only so i can follow along.

$count = 0
$file = FileOpen("test.txt", 0)
$ID_Found = 0
$HLfileLst = FileOpen("testwrite.txt", 2)

If $file = -1 Then
   MsgBox(64, "Error!", "HLscan log could not be opened!")
   Exit
EndIf

While 1
   $lines = FileReadLine($file)
   If @error = -1 Then ExitLoop
   If StringInStr($lines, "ID:") Then
      $ID_Found = 1
      $IDline = StringSplit($lines, "-")
   EndIf
   If $ID_Found = 1 Then $count = $count + 1
   If $count = 4 Then
      MsgBox(0, 'ID# & its THIRD LINE', $IDline[1] & "," & $lines)
      FileWriteLine($HLfileLst, $IDline[1] & "," & $lines)
      $count = 0
      $ID_Found = 0
   EndIf
Wend

FileClose($HLfileLst)
FileClose($file)
Run("Notepad.exe testwrite.txt",@ScriptDir)
Exit

This is my starting doc:

ID: 0x9xxx2 - not needed - not needed

not needed

not needed

3rd line of 1st ID

not needed

ID: 0x9xxx1 - not needed - not needed

not needed

not needed

3rd line of 2nd ID

not needed

not needed

this is what i get:

ID: 0x9xxx2 , 3rd line of 1st ID

ID: 0x9xxx1 , 3rd line of 2nd ID

thx JdeB for helping me

Take Care

A.B.Ames

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...