Sign in to follow this  
Followers 0
KMT

how to delete lines from a file

6 posts in this topic

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!

Share this post


Link to post
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 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
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?"...

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

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!"

Share this post


Link to post
Share on other sites

This works.

Thank you very much for your help! Posted Image

:)

Share this post


Link to post
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.
;

Share this post


Link to post
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
Sign in to follow this  
Followers 0