Jump to content

Help with removing X line in text


Recommended Posts

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

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 by Musashi

Musashi-C64.png

"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

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 here
RegExp tutorial: enough to get started
PCRE 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

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 by abberration
Link to comment
Share on other sites

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

🤪 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 here
RegExp tutorial: enough to get started
PCRE 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

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.

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"

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

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)

 

Link to comment
Share on other sites

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

Fair point mi\Kell

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 here
RegExp tutorial: enough to get started
PCRE 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

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 here
RegExp tutorial: enough to get started
PCRE 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

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...