byoda Posted June 16, 2010 Share Posted June 16, 2010 All, I want to open a file, search the file for a keyword, grab 10 lines after the keyword and then write it into another file. I am able to open, search and locate the keyword, but I cant figure out how to get the 10 lines after the keyword and then writting them into a file. Thanks. $file=FileOpen("C:\file.txt") $read=FileRead($file) If StringRegExp($read, "keyword")Then ;Copy the next 10 lines ;Write into another file Link to comment Share on other sites More sharing options...
clonetrooper9494 Posted June 16, 2010 Share Posted June 16, 2010 (edited) This would how I would attack the problem. I would use StringSplit and use $var[2] and split that by @CR, then from that array loop through it combine $var[1] through $var[11] in to one variable and save it to another file. something like... $filepart = StringSplit($file_contents,"keyword") $lines = StringSplit($filepart[2],@CR) $compiled_array = "" For $i = 0 To 10 $compiled_array &= $lines[$i+1] $ @CR;add back the line break Next My code isn't perfect though Edited June 16, 2010 by clonetrooper9494 Link to comment Share on other sites More sharing options...
UEZ Posted June 16, 2010 Share Posted June 16, 2010 (edited) Here my 1st idea: Local $i, $x, $line, $file, $keyword $keyword = "Line15" $file = FileOpen("c:\file.txt", 0) $new_file = FileOpen("c:\file_new.txt", 2) $i = 2 While 1 $line = FileReadLine($file) If @error = -1 Then ExitLoop If StringInStr($line, $keyword) > 0 Then For $x = $i To $i + 10 FileWriteLine($new_file, FileReadLine($file, $x)) Next ExitLoop EndIf $i += 1 WEnd FileClose($file) FileClose($new_file) BR, UEZ Edited June 16, 2010 by UEZ Please don't send me any personal message and ask for support! I will not reply! Selection of finest graphical examples at Codepen.io The own fart smells best! ✌Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!¯\_(ツ)_/¯ ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ Link to comment Share on other sites More sharing options...
byoda Posted June 16, 2010 Author Share Posted June 16, 2010 Here my 1st idea: Local $i, $x, $line, $file, $keyword $keyword = "Line15" $file = FileOpen("c:\file.txt", 0) $new_file = FileOpen("c:\file_new.txt", 2) $i = 2 While 1 $line = FileReadLine($file) If @error = -1 Then ExitLoop If StringInStr($line, $keyword) > 0 Then For $x = $i To $i + 10 FileWriteLine($new_file, FileReadLine($file, $x)) Next ExitLoop EndIf $i += 1 WEnd FileClose($file) FileClose($new_file) BR, UEZ Worked like a champ. Thank you very much. Link to comment Share on other sites More sharing options...
byoda Posted June 17, 2010 Author Share Posted June 17, 2010 Here my 1st idea: Local $i, $x, $line, $file, $keyword $keyword = "Line15" $file = FileOpen("c:\file.txt", 0) $new_file = FileOpen("c:\file_new.txt", 2) $i = 2 While 1 $line = FileReadLine($file) If @error = -1 Then ExitLoop If StringInStr($line, $keyword) > 0 Then For $x = $i To $i + 10 FileWriteLine($new_file, FileReadLine($file, $x)) Next ExitLoop EndIf $i += 1 WEnd FileClose($file) FileClose($new_file) BR, UEZ One last thing. If I run 2 of these in a row, the first ouput in the file is erased. I thought it might be the FileWriteLine, so I changed it to FileWrite, but it still erases the first output. Any suggestions? Thanks. Link to comment Share on other sites More sharing options...
sahsanu Posted June 17, 2010 Share Posted June 17, 2010 One last thing. If I run 2 of these in a row, the first ouput in the file is erased. I thought it might be the FileWriteLine, so I changed it to FileWrite, but it still erases the first output. Any suggestions? Thanks. You should open the file in mode 1 -> "Write mode (append to end of file)". Take a look to FileOpen help $new_file = FileOpen("c:\file_new.txt", 1) Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now