Jump to content

Copy every nth line to new file?


mdwerne
 Share

Recommended Posts

Hello,

I'm looking for a place to start.

What I'm trying to do is take a text file...read it in and copy every 6th line to a new text document.

I've looked at FileReadLine but I'm not exactly what sort of a loop would look at and spit out every 6th line.

Does anyone have a suggestion as to where in the help I might try looking, or maybe link to a similar request in the forum? I searched but could not find anything similar.

Thanks for any suggestions,

-Mike

P.S.

The original document is 25 megs in size. :)

Link to comment
Share on other sites

Hello,

I'm looking for a place to start.

What I'm trying to do is take a text file...read it in and copy every 6th line to a new text document.

I've looked at FileReadLine but I'm not exactly what sort of a loop would look at and spit out every 6th line.

Does anyone have a suggestion as to where in the help I might try looking, or maybe link to a similar request in the forum? I searched but could not find anything similar.

Thanks for any suggestions,

-Mike

P.S.

The original document is 25 megs in size. >_<

So you get a For/Next loop with "Step 6" in it, and use the iterated variable as the line number? Didn't you try it already? What code did you try, and what happened?

:)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

So you get a For/Next loop with "Step 6" in it, and use the iterated variable as the line number? Didn't you try it already? What code did you try, and what happened?

:)

Here is where I am but I don't understand enough to know what I'm doing wrong:

$file = FileOpen("inFile.txt", 0)
$outfile = FileOpen("outFile.txt", 1)
; Check if file opened for reading OK
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

; Read in lines of text until the EOF is reached
While 1
    $line = FileReadLine($file)
    If @error = -1 Then ExitLoop
    for $i = 1 Step 6
    FileWriteLine($outfile, $line)
    next
Wend

FileClose($file)
FileClose($outfile)
Link to comment
Share on other sites

look in help under filereadline, that should give you a clue. move that to the for loop, unless you want line 1 your for loop needs to be for $i = 6 step 6. and get rid of the while loop.

Ok...now I'm here. Not sure how to remove the while loop, still looking.

Using the code below...from my original 25 meg file, I generated > than a 1 gig file before I quit the script. :)

#include <file.au3>
$CountLines = _FileCountLines("inFile.txt")
$file = FileOpen("inFile.txt", 0)
$outfile = FileOpen("outFile2.txt", 1)
; Check if file opened for reading OK
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

; Read in lines of text until the EOF is reached
While 1
    $line = FileReadLine($file)
    If @error = -1 Then ExitLoop
    for $i = 6 to $CountLines Step 6
    FileWriteLine($outfile, $line)
    next
Wend

FileClose($file)
FileClose($outfile)
Link to comment
Share on other sites

Ok...now I'm here. Not sure how to remove the while loop, still looking.

Using the code below...from my original 25 meg file, I generated > than a 1 gig file before I quit the script. :)

#include <file.au3>
$CountLines = _FileCountLines("inFile.txt")
$file = FileOpen("inFile.txt", 0)
$outfile = FileOpen("outFile2.txt", 1)
; Check if file opened for reading OK
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

; Read in lines of text until the EOF is reached
While 1
    $line = FileReadLine($file)
    If @error = -1 Then ExitLoop
    for $i = 6 to $CountLines Step 6
    FileWriteLine($outfile, $line)
    next
Wend

FileClose($file)
FileClose($outfile)
do something like this

#include <file.au3>
$file = FileOpen("inFile.txt", 0)
$outfile = FileOpen("outFile2.txt", 1)
; Check if file opened for reading OK
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

; Read in lines of text until the EOF is reached

    for $i = 6  to 0 Step 6;$i will never = 0 so this will make it go until FileReadLine returns an error
        $line = FileReadLine($file,$i);the $i is the line you want to read (6,12,18,ect.)
    If @error = -1 Then ExitLoop; exits the loop when done
    FileWriteLine($outfile, $line)
    next


FileClose($file)
FileClose($outfile)

Giggity

Link to comment
Share on other sites

Here is where I am but I don't understand enough to know what I'm doing wrong:

$file = FileOpen("inFile.txt", 0)
$outfile = FileOpen("outFile.txt", 1)
; Check if file opened for reading OK
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

; Read in lines of text until the EOF is reached
While 1
    $line = FileReadLine($file)
    If @error = -1 Then ExitLoop
    for $i = 1 Step 6
    FileWriteLine($outfile, $line)
    next
Wend

FileClose($file)
FileClose($outfile)
More like this:
; Input file
$sInFile = "inFile.txt"

; Open output file
$hOutFile = FileOpen("outFile.txt", 1)
If $hOutFile = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

; Read in lines of text until the EOF is reached
For $i = 1 To _FileCountLines($sInFile) Step 6
    $sLine = FileReadLine($sInFile, $i)
    If @error Then ExitLoop
    FileWriteLine($hOutFile, $sLine)
Next

; Close output file
FileClose($hOutFile)

:)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

do something like this

#include <file.au3>
$file = FileOpen("inFile.txt", 0)
$outfile = FileOpen("outFile2.txt", 1)
; Check if file opened for reading OK
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

; Read in lines of text until the EOF is reached

    for $i = 6  to 0 Step 6;$i will never = 0 so this will make it go until FileReadLine returns an error
        $line = FileReadLine($file,$i);the $i is the line you want to read (6,12,18,ect.)
    If @error = -1 Then ExitLoop; exits the loop when done
    FileWriteLine($outfile, $line)
    next


FileClose($file)
FileClose($outfile)
Humm...I tried this and it finishes in 1.1 seconds and the output file contains no data. But the for loop makes sense...now I see how you removed the While loop. Thank you! I will keep trying.
Link to comment
Share on other sites

Humm...I tried this and it finishes in 1.1 seconds and the output file contains no data. But the for loop makes sense...now I see how you removed the While loop. Thank you! I will keep trying.

OK...I added the $CountLines back in and this seems to work...taking a long time (5 minutes so far) but progressing.

#include <file.au3>
$message = "Please select your text file."
$file = FileOpenDialog($message, @WindowsDir & "\", "Text files (*.txt)", 1 + 4)

If @error Then
    MsgBox(4096, "", "No File chosen")
Else
    $file = StringReplace($file, "|", @CRLF)
;MsgBox(4096,"","You chose " & $file)
EndIf

$CountLines = _FileCountLines($file)
$outfile = FileOpen("outFile.txt", 1)

If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

For $i = 6 To $CountLines Step 6;$i will never = 0 so this will make it go until FileReadLine returns an error
    $line = FileReadLine($file, $i);the $i is the line you want to read (6,12,18,ect.)
    If @error = -1 Then ExitLoop; exits the loop when done
    FileWriteLine($outfile, $line)
Next

FileClose($file)
FileClose($outfile)

Thank you PsaltyDS and youknowwho4eva for the help!!!

Link to comment
Share on other sites

OK...I added the $CountLines back in and this seems to work...taking a long time (5 minutes so far) but progressing.

#include <file.au3>
$message = "Please select your text file."
$file = FileOpenDialog($message, @WindowsDir & "\", "Text files (*.txt)", 1 + 4)

If @error Then
    MsgBox(4096, "", "No File chosen")
Else
    $file = StringReplace($file, "|", @CRLF)
;MsgBox(4096,"","You chose " & $file)
EndIf

$CountLines = _FileCountLines($file)
$outfile = FileOpen("outFile.txt", 1)

If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

For $i = 6 To $CountLines Step 6;$i will never = 0 so this will make it go until FileReadLine returns an error
    $line = FileReadLine($file, $i);the $i is the line you want to read (6,12,18,ect.)
    If @error = -1 Then ExitLoop; exits the loop when done
    FileWriteLine($outfile, $line)
Next

FileClose($file)
FileClose($outfile)

Thank you PsaltyDS and youknowwho4eva for the help!!!

So I'm two hours into processing my file and it's still going. There are 350,000 lines to go through...does it make sense that it's taking this long? Just for reference, a typical line looks like this:

123123  96.504  10.05550  5142486  5050726   -456240    0.0056  0.1056
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...