User751139 Posted June 10, 2020 Share Posted June 10, 2020 Hello, I'm hoping someone can help me finish this: Basically need to have autoIT remove the Last and the 4th last line off a log file as it contains personal info. I can get the last line removed, but can't figure out what I need to do for the 4th last: This works so far to remove the last line: #include <Constants.au3> #Include <File.au3> $sLogPath = 'Debug.txt' #include <Array.au3> #include <Word.au3> $iCount = _FileCountLines($sLogPath) _FileWriteToLine($sLogPath, $iCount, '', 1) $s = FileRead ("Debug.txt") MsgBox ($MB_SYSTEMMODAL,"Log file contents",StringMid($s,StringInStr($s,@CRLF,$STR_CASESENSE,-15)+1)) Eg of log file below: Line 1 Line 2 Line 3 Line 4 <-- need to remove 4th last line Line 5 Line 6 Line 7 <-- removes last line The log file above doesn't always have 7 lines, sometimes there's a few hunded. But the main goal is the same: Always need to remove the last and the 4th last line. Thank you Link to comment Share on other sites More sharing options...
Musashi Posted June 10, 2020 Share Posted June 10, 2020 (edited) 48 minutes ago, User751139 said: I can get the last line removed, but can't figure out what I need to do for the 4th last: One approach (of several) : You could read the debug.txt with _FileReadToArray into an array. Then remove the respective lines with _ArrayDelete . Finally write back the array with _FileWriteFromArray . Quick example : #include <File.au3> #include <Array.au3> Local $aDebugArr, $sFilePath = @ScriptDir & "\debug.txt" If _FileReadToArray($sFilePath, $aDebugArr, BitOR($FRTA_NOCOUNT, $FRTA_ENTIRESPLIT)) Then ; Before : _ArrayDisplay($aDebugArr) ; *** just for display _ArrayDelete($aDebugArr, UBound($aDebugArr) - 4) ; delete 4th last line _ArrayDelete($aDebugArr, UBound($aDebugArr) - 1) ; delete last line ; After : _ArrayDisplay($aDebugArr) ; *** just for display _FileWriteFromArray($sFilePath, $aDebugArr) Else ConsoleWrite("! Error " & @CRLF) EndIf debug.txt Edited June 10, 2020 by Musashi "In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move." Link to comment Share on other sites More sharing options...
jchd Posted June 10, 2020 Share Posted June 10, 2020 One-liner: Local $s = _ "Line 1" & @CRLF & _ "Line 2" & @CRLF & _ "Line 3" & @CRLF & _ "Line 4 <-- need to remove 4th last line" & @CRLF & _ "Line 5" & @CRLF & _ "Line 6" & @CRLF & _ "Line 7 <-- removes last line" $s = StringRegExpReplace($s, "(?ms)(.*)(^\N+\R)((?:^\N+\R){2})(^\N+\Z)", "$1$3") ConsoleWrite($s & @LF) This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe hereRegExp tutorial: enough to get startedPCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta. SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt) Link to comment Share on other sites More sharing options...
abberration Posted June 10, 2020 Share Posted June 10, 2020 (edited) Or you can add another _filewritetoline just like the one you have, subtracting 3 from $iCount (1 line has already been deleted, so you only need to go to the 3rd from the bottom). Edit: no, maybe you should subtract 4 since $iCount has not changed. Play with it and see. Edited June 10, 2020 by abberration Easy MP3 | Software Installer | Password Manager Link to comment Share on other sites More sharing options...
User751139 Posted June 10, 2020 Author Share Posted June 10, 2020 Thanks fellas! Link to comment Share on other sites More sharing options...
mikell Posted June 10, 2020 Share Posted June 10, 2020 An other one-liner Local $s = _ "Line 1" & @CRLF & _ "Line 2" & @CRLF & _ "Line 3" & @CRLF & _ "Line 4 <-- need to remove 4th last line" & @CRLF & _ "Line 5" & @CRLF & _ "Line 6" & @CRLF & _ "Line 7 <-- removes last line" $s = StringRegExpReplace($s, "(?m)(^\N+\R){3}\K(?1)|(^\N+\R?\Z)", "") Msgbox(0,"", $s) Link to comment Share on other sites More sharing options...
jchd Posted June 10, 2020 Share Posted June 10, 2020 🤪 You're really in love with \K, albeit having been hit before. This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe hereRegExp tutorial: enough to get startedPCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta. SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt) Link to comment Share on other sites More sharing options...
Malkey Posted June 11, 2020 Share Posted June 11, 2020 Looking at the example in the opening post, conventionally, declaring a variable isn't normally placed inbetween the "#include" statements. Included are two more one-liners. expandcollapse popupLocal $s = _ "Line 1" & @CRLF & _ "Line 2" & @CRLF & _ "Line 3" & @CRLF & _ "Line 4" & @CRLF & _ "Line 5 <-- need to remove 4th last line" & @CRLF & _ "Line 6" & @CRLF & _ "Line 7" & @CRLF & _ "Line 8 <-- removes last line" ConsoleWrite("--- jchd ---( Trailing @CRLF or clears last line)" & @CRLF) $s1 = StringRegExpReplace($s, "(?ms)(.*)(^\N+\R)((?:^\N+\R){2})(^\N+\Z)", "$1$3") ConsoleWrite($s1 & @LF) ConsoleWrite("--- mikell --- (Trailing @CRLF + Removes 4th line from beginning)" & @CRLF) $s2 = StringRegExpReplace($s, "(?m)(^\N+\R){3}\K(?1)|(^\N+\R?\Z)", "") ConsoleWrite($s2 & @LF) ConsoleWrite("------ RE ---------" & @CRLF) $s3 = StringRegExpReplace($s, "(\N+\R)((?1)\N+)(\R\N+\R*$)", "$2") ConsoleWrite($s3 & @LF) ConsoleWrite("--- StringMid ----" & @CRLF) $s4 = StringMid($s, 1, StringInStr($s, @CRLF, 0, -4) - 1) & StringMid($s, StringInStr($s, @CRLF, 0, -3), StringInStr($s, @CRLF, 0, -1) - StringInStr($s, @CRLF, 0, -3)) ConsoleWrite($s4 & @LF) #cs ; Returns:- --- jchd ---( Trailing @CRLF or clears last line) Line 1 Line 2 Line 3 Line 4 Line 6 Line 7 --- mikell --- (Trailing @CRLF + Removes 4th line from beginning) Line 1 Line 2 Line 3 Line 5 <-- need to remove 4th last line Line 6 Line 7 ------ RE --------- Line 1 Line 2 Line 3 Line 4 Line 6 Line 7 --- StringMid ---- Line 1 Line 2 Line 3 Line 4 Line 6 Line 7 #ce Link to comment Share on other sites More sharing options...
Nine Posted June 11, 2020 Share Posted June 11, 2020 another way to skin the cat : #include <Array.au3> Local $source = StringSplit ( _ "Line 1" & @CRLF & _ "Line 2" & @CRLF & _ "Line 3" & @CRLF & _ "Line 4" & @CRLF & _ "Line 5 <-- need to remove 4th last line" & @CRLF & _ "Line 6" & @CRLF & _ "Line 7" & @CRLF & _ "Line 8 <-- removes last line", @CRLF, $STR_ENTIRESPLIT) Local $final = _ArrayToString ($source, @CRLF, 1, $source[0]-4) & @CRLF & _ArrayToString ($source, @CRLF, $source[0]-2, $source[0]-1) ConsoleWrite ($final & @CRLF) “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
mikell Posted June 11, 2020 Share Posted June 11, 2020 50 minutes ago, Malkey said: --- mikell --- (Trailing @CRLF + Removes 4th line from beginning) Wow I misread the requirements Link to comment Share on other sites More sharing options...
mikell Posted June 14, 2020 Share Posted June 14, 2020 On 6/10/2020 at 10:48 PM, jchd said: You're really in love with \K, albeit having been hit before. Despite this former mishap with \K , I was sure that it could be used here Local $s = _ "Line 1" & @CRLF & _ "Line 2" & @CRLF & _ "Line 3" & @CRLF & _ "Line 4" & @CRLF & _ "Line 5 <-- need to remove 4th last line" & @CRLF & _ "Line 6" & @CRLF & _ "Line 7" & @CRLF & _ "Line 8 <-- removes last line" & @CRLF & @CRLF $s = StringRegExpReplace($s, '(?ms).*?\K(^\N+\R*)(?=((?1){3}\z)|\z)', "") Msgbox(0,"",$s) Link to comment Share on other sites More sharing options...
jchd Posted June 14, 2020 Share Posted June 14, 2020 Fair point mi\Kell FrancescoDiMuro and mikell 2 This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe hereRegExp tutorial: enough to get startedPCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta. SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt) Link to comment Share on other sites More sharing options...
mikell Posted June 14, 2020 Share Posted June 14, 2020 Nice shot. Lateral thinking inside ? Link to comment Share on other sites More sharing options...
jchd Posted June 14, 2020 Share Posted June 14, 2020 Yes, I scartastically set ground for calling you just ell (less typing). This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe hereRegExp tutorial: enough to get startedPCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta. SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt) 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