Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Find a certain sentence in a TXT file and copy the contents after it (Loop)



  • Please log in to reply
5 replies to this topic
tsr222
  • Members
  • 7 posts
  • Last active: Sep 03 2013 05:36 PM
  • Joined: 17 Nov 2012

I have a series of log files. They all always have the same line of text at some point during them: Session starting

 

I am trying to work out a script that would detect this line, "Session starting" and subsequently copy every line of text after it to a separate file every minute or so. I can work out the timer and some of the other aspects of the script, but I just can't seem to figure out how to find this "Session Starting" line, AND copy all the contents after it to another file. The log file is constantly written to, so I do not know if additional lines being added during the course of this loop poses any difficulty in the script.

 

Any help on this would be appreciated!



axlar
  • Members
  • 196 posts
  • Last active: Dec 04 2015 08:50 AM
  • Joined: 29 Nov 2012

Hi tsr22,

 

I am trying to work out a script that would detect this line

Loop (read file contents) will be help. There is sample that search string.

http://www.autohotke...oopReadFile.htm

Loop, read, C:\Docs\Address List.txt, C:\Docs\Family Addresses.txt
{
    IfInString, A_LoopReadLine, family, FileAppend, %A_LoopReadLine%`n
} 

 

  subsequently copy every line of text after it to a separate file

copy all the contents after it to another file

FileAppend will do that job.

 

every minute

See SetTimer. If your script doesn't work, please post it.



faqbot
  • Members
  • 997 posts
  • Last active:
  • Joined: 10 Apr 2012
If the log file(s) grows very rapidly and thus very large AutoHotkey might not be the best solution and I would recommend a dedicated tail app. But if you want to check once a minute AutoHotkey should be able to manage that (provided the log file isn't really huge) You could search the forum for TAIL or have a look here https://ahknet.autoh...foldersandfiles In general anything to do with text files in AutoHotkey: The TF library is worth a look and can certainly read all text from a file after a certain line https://ahknet.autoh...ugov/tf-lib.htm

tsr222
  • Members
  • 7 posts
  • Last active: Sep 03 2013 05:36 PM
  • Joined: 17 Nov 2012

Hello,

 

Thanks Axlar and faqbot for the responses. I managed to cobble together a detection script from an old script I have which detects that the line is present and attempts to append to a new file. The new file gets created, although I cannot seem to find how to append contents that follow my initial inquiry (it just pastes the whole file at the moment naturally). 

 

Code:

sFile := "C:\TestLogs\*.log"
Loop, {
Loop, %sFile%
{
FileRead, sIn, %A_LoopFileLongPath%
if RegExMatch(sIn, "i)Session starting") {
	
	FileDelete, C:\TestLogs\Outputs\*.log
	FileAppend, %sIn%, C:\TestLogs\Outputs\Output.log
	sIn = ; Memory cleared
}
; Placeholder for another possible sentence and output
}
Sleep 5000
}

 

Now I know I am using FileRead in this instance, but I figured since I already had something similar, I would run with it. My question is:

 

How do I select the contents of the file that follow my initial text search target as well as the initially found text? Is this done through a line detection method?

Any help is appreciated.

 

@faqbot - What size file constitutes large? These particular files will never get larger than 200kb or so. Is that a safe size to work with? Thanks.



axlar
  • Members
  • 196 posts
  • Last active: Dec 04 2015 08:50 AM
  • Joined: 29 Nov 2012

Hello,

 

Thanks Axlar and faqbot for the responses. I managed to cobble together a detection script from an old script I have which detects that the line is present and attempts to append to a new file. The new file gets created, although I cannot seem to find how to append contents that follow my initial inquiry (it just pastes the whole file at the moment naturally). 

 

Code:

sFile := "C:\TestLogs\*.log"
Loop, {
Loop, %sFile%
{
FileRead, sIn, %A_LoopFileLongPath%
if RegExMatch(sIn, "i)Session starting") {
	
	FileDelete, C:\TestLogs\Outputs\*.log
	FileAppend, %sIn%, C:\TestLogs\Outputs\Output.log
	sIn = ; Memory cleared
}
; Placeholder for another possible sentence and output
}
Sleep 5000
}

 

Now I know I am using FileRead in this instance, but I figured since I already had something similar, I would run with it. My question is:

 

How do I select the contents of the file that follow my initial text search target as well as the initially found text? Is this done through a line detection method?

Any help is appreciated.

 

@faqbot - What size file constitutes large? These particular files will never get larger than 200kb or so. Is that a safe size to work with? Thanks.

 

 

Is this done through a line detection method?

Fileread into "sIn" is whole text... Maybe "Loop, Read file contents" do that.

 

I think following line detection method is better(but I cannot imagine where should I delete file.):

sFile := "C:\TestLogs\*.log"
Loop, {
    FileDelete, C:\TestLogs\Outputs\*.log ; is it ok here?
    Loop, %sFile%
    {
        ; FileDelete, C:\TestLogs\Outputs\*.log
        Loop, read, %A_LoopFileLongPath%
        {
            sIn := A_LoopReadLine
            if RegExMatch(sIn, "i)Session starting") {
                FileAppend, %sIn%`n, C:\TestLogs\Outputs\Output.log
                sIn = ; Memory cleared
            }
        }
        ; Placeholder for another possible sentence and output
    }
    Sleep 5000
}


 

 

In other solution, I suggest you to try to use GnuWin32 grep and tail in command line...
grep -e "Session start" C:\TestLogs\*.log | tail -F | tee C:\TestLogs\Output\Output.log

 

 



tsr222
  • Members
  • 7 posts
  • Last active: Sep 03 2013 05:36 PM
  • Joined: 17 Nov 2012
✓  Best Answer

Wanted to thank you Axlar for your help, and faqbot for turning me onto TF(lib) which all helped me get this script going the way I wanted. Much appreciated!

 

Final Script:

Menu, Tray, Icon, config.ico
#include tf.ahk

;CLEARS THE LOGS FOLDER BY MOVING ALL LOGS TO ARCHIVE FOLDER
FileMove, C:\TestLogs\*.log, C:\TestLogs Archive\
	sleep 100
FileDelete, C:\TestLogs\*.log
	sleep 100
	
Loop
{

FileDelete, C:\TestLogs for FTP\CCDAP_Log.txt
FileCopy, C:\TestLogs\*.log, C:\TestLogs for FTP\Copy for FTP\Output.log, 1
	
Sleep 250

		MyVar2:=TF_Find("C:\TestLogs for FTP\Copy for FTP\Output.log", "", "", "Light frames start") ; return first line number with Light frames start in it
			;MsgBox % MyVar2
			
		MyVar3:=TF_Find("C:\TestLogs for FTP\Copy for FTP\Output.log", "", "", "Light frames end") ; return first line number with Light frames end in it
			;MsgBox % MyVar3
			
		Lines2:=TF_ReadLines("C:\TestLogs for FTP\Copy for FTP\Output.log",MyVar2,MyVar3) ; Read lines MyVar to end of file, store result in variable
			;MsgBox % Lines2
	
	;FileAppend, %Lines2%`n`n, C:\TestLogs for FTP\CCDAP_Log.txt
	
Sleep 250

		MyVar:=TF_Find("C:\TestLogs for FTP\Copy for FTP\Output.log", "", "", "Session starting") ; return first line number with Session starting in it
			;MsgBox % MyVar
			
		Lines:=TF_ReadLines("C:\TestLogs for FTP\Copy for FTP\Output.log",MyVar) ; Read lines MyVar to end of file, store result in variable
			;MsgBox % Lines
	
	FileAppend, %Lines2%`n`n %Lines%, C:\TestLogs for FTP\CCDAP_Log.txt

; DELAY BETWEEN UPDATES
sleep 30000
}

 

Some delays and whatnot are just placeholders or being tested, but I ran the script for 8 hours last night and everything performed as it should. Thanks again!