Best way to get the last nth lines from a large text file

General Help regarding HMG, Compilation, Linking, Samples

Moderator: Rathinagiri

HGAutomator
Posts: 197
Joined: Thu Jul 16, 2020 5:42 pm
DBs Used: DBF

Re: Best way to get the last nth lines from a large text file

Post by HGAutomator »

Thanks, Franco.

Actually, I'm doing this all in text mode, with the parameters from the command line. I'll post a generic version of the code, after it's finished.

But right now, it's working fine, and is generating the expected file.
franco wrote: Fri Jun 25, 2021 3:40 pm I had to modify again. Look at last post I am going to modify. We need a skip after finding your rec.
I added a msgbox to check.
HGAutomator
Posts: 197
Joined: Thu Jul 16, 2020 5:42 pm
DBs Used: DBF

Re: Best way to get the last nth lines from a large text file

Post by HGAutomator »

There's still a couple of bugs to be worked out .e.g a blank line at the top of the output file, and an extra line printed (besides the blank line).

But this works ok. Thanks for collaborating on this, everyone:

Batch file to call the function:

Extract_Lines_from_LargeTextFiles.exe "SourceFile.txt" "TargetFile.txt" "500"


Code:

Code: Select all

#include "minigui.ch"
#include "hmg.ch"
#include "FILEIO.CH"


Function Extract_Lines_from_LargeTextFiles( Source_FileName_s, Target_FileName_s, Number_of_Lines_to_Extract_s  )

Local Number_of_Lines_to_Extract_n := 0
Local First_Needed_Record_n := 0
Local Line_s := ""
Local Row_Number_n := 0
Local Current_Recno_n := 0
Local Total_Number_Of_Records_n := 0
Local Eof_b := .F.
Local Error_o


If Source_FileName_s == Nil .OR. Target_FileName_s == Nil
	? "Syntax is"
	? "Extract_Lines_from_LargeTextFiles (Source FileName) (Target FileName) (Optional: Number of lines to extract from the end of the file)"
	Close all
	Return
EndIf

DEFAULT Number_of_Lines_to_Extract_s TO "1000"
Number_of_Lines_to_Extract_n := Val( Number_of_Lines_to_Extract_s )


TRY

	HB_Fuse(Source_FileName_s)      
	Current_Recno_n := hb_FRecNo()
	Total_Number_Of_Records_n := HB_FlastRec()
	If Total_Number_Of_Records_n  > Number_of_Lines_to_Extract_n
	 	First_Needed_Record_n := Total_Number_Of_Records_n - Number_of_Lines_to_Extract_n
		HB_FGoto(First_Needed_Record_n) 
	else
		HB_FGoTop()
	EndIf
	Current_Recno_n := hb_FRecNo()
	SET CONSOLE OFF
	SET PRINTER ON
	SET PRINTER TO (Target_FileName_s)
	DO WHILE ! hb_FAtEof()
		Line_s := HB_FReadLN()
		HB_FSkip()
		? Line_s
		Loop
	ENDDO
	SET PRINTER OFF
	SET PRINTER TO
	SET CONSOLE ON

	HB_FGoBottom()	
	HB_FUse()

CATCH Error_o


		? "Error: ", Error_o:Description
		Close all
		Return

	END


Return Nil
franco
Posts: 877
Joined: Sat Nov 02, 2013 5:42 am
DBs Used: DBF
Location: Canada

Re: Best way to get the last nth lines from a large text file

Post by franco »

My last post is now correct. We had to add 1 to last record because if you take away from last record you get 1 to many.
By adding 1 we get to end of file then take away the number we want.
All The Best,
Franco
Canada
Post Reply