Jump to content

how to delete lines from a file


 Share

Recommended Posts

Hello guys!

I'm very noob at programming, but I have to do a task.

What I have to do is deleting every second line (or odd/even lines) from a simple (but big) data file. It has more than 30000 lines, so I won't do it manually.

Please help me, what commands should I use to do this?!

Thanks in advance!

Link to comment
Share on other sites

Something like this (untested) script. The file is read into an array end every second record - starting with record 2 - is written to an output file.

#include <file.au3>
Dim  $aRecords
If Not _FileReadToArray("Input.txt",$aRecords) Then
   MsgBox(4096,"Error", " Error reading file to Array      error:" & @error)
   Exit
EndIf
$hFile = FileOpen("Output.txt",2)
For $x =  2 to $aRecords[0] step 2 
    FileWrite($hFile,$aRecords[$x])
Next
FileClose($hFile)

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Something like this (untested) script. The file is read into an array end every second record - starting with record 2 - is written to an output file.

Thanks.

Seems to be ok, but this confused the lines.

So it should look like:

"line1

line3

line5..."

and it looks like:

"line 1 line 3? line 5?"...

Link to comment
Share on other sites

Thanks.

Seems to be ok, but this confused the lines.

So it should look like:

"line1

line3

line5..."

and it looks like:

"line 1 line 3? line 5?"...

Change the FileWrite() to FileWriteLine() or better yet.

#include <file.au3>
Dim  $aRecords
If Not _FileReadToArray("Input.txt",$aRecords) Then
   MsgBox(4096,"Error", " Error reading file to Array      error:" & @error)
   Exit
EndIf

$sText = ""
For $x =  2 to $aRecords[0] step 2
    $sText &= $aRecords[$x] & @CRLF
Next

If $sText Then
    $hFile = FileOpen("Output.txt",2)
    FileWrite($hFile,StringStripWS($sText, 2))
    FileClose($hFile)
EndIf

Also, since the keyword Dim is being phased out, you should get in the habit of using either Global or Local to replace Dim. That particularily applies in a case like this where Dim is not Dim(ension)ing anything. It's an empty string.

Edited by GEOSoft

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

This is another method that appears to works.

I am guessing this is faster. I haven't done any comparative speed testing.

;
Local $sFileIn = "test.txt"
Local $sFileOut = "TestOut.txt"

If FileExists($sFileIn) = 0 Then Exit

Local $filtxt = FileRead($sFileIn)
;Local $sStr = StringRegExpReplace($filtxt, "((.*\v+)|(.*$))((.*\v+)|(.*$))", "\2") ; Back-reference, \2, returns 1st line, 3rd line , 5th, ...
Local $sStr = StringRegExpReplace($filtxt, "((.*\v+)|(.*$))((.*\v+)|(.*$))", "\4") ; Back-reference, \4, returns 2nd line, 4th line , 6th, ...

Local $hFile = FileOpen($sFileOut, 10)
FileWrite($hFile, $sStr)
FileClose($hFile)

;ShellExecute($sFileOut) ; Opens file with the application associated with the file extension.
;
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...