Jump to content

Delete blank Lines With a space in a text file


Recommended Posts

Ok I'm stumped. I have been trying to delete 3 blank lines at the top of my text file.

Each line has one space in it. Here is the code i have so far

Global $s_infile = "C:\Config.Msi\Backup\log\Test11.txt"
Global $s_outfile = "C:\Config.Msi\Backup\log\Test11.txt"
Global $s_string = FileRead($s_infile)
StringRegExpReplace($s_string, "([:space:]\v{3,}", "")
Global $h_open = FileOpen($s_outfile, 2)
FileWrite($h_open, $s_string)
FileClose($h_open)

Your help would be greatly appreciated

Link to comment
Share on other sites

  • Developers

You are not saving the result of StringRegExpReplace() and write the original string to the output file.

Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Ok I changed it to the following but still doesnt work

Global $s_infile = "C:\Config.Msi\Backup\log\Test11.txt"
Global $s_outfile = "C:\Config.Msi\Backup\log\Test11.txt"
Global $s_string = FileRead($s_infile)
$s_string = StringRegExpReplace($s_string, "([:space:]\v{3,}", "")
Global $h_open = FileOpen($s_outfile, 2)
FileWrite($h_open, $s_string)
FileClose($h_open)
Link to comment
Share on other sites

$sStr = "This is some string" & @CRLF & @CRLF
$sStr &= "Which was followed by an empty line" & @CRLF & @CRLF & " " & @CRLF
$sStr &= "And line containing only a string."

$sHold = $sStr & @CRLF & @CRLF

$sStr = StringRegExpReplace($sStr, "\h+\v+", "")

$sHold &= $sStr

MsgBox(0, "Result", $sHold)

EDIT: Solved a case-sensitivity issue and removed the note about it. This should be fine as is.

EDIT 2:

Of course after re-reading your problem, Jos and I both missed the obvious since you did mention they were only at the beginning of the file.

$sStr = StringStripWS($sStr, 1)

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

Thank you both for your help, I did not see the latest reply until today.

I apologize for the late reply,

This is what I emplimented:

The first string removes the string statement removes some blank lines ( and i did find they are through out)

Then Next 2 to StringRight lines removes any trailing lines and moves the carrage return to the end of the last sentence.

My issues is that i have to run it 4 times to remove all the blank lines. I would like to cleanup this code some to make it better.

I would really like it to test for more blank lines and run until no more exist. I would appreciate any help you could give.

Below is my feable attempt:

Func _BlankS()
    Global $s_infile = "C:\test1.txt"
    Global $s_outfile = "C:\test1.txt"
    Global $s_string = FileRead($s_infile)
    $s_string = StringRegExpReplace($s_string, "(\s\r\n){0,3}", "")
    If StringRight($s_string, 1) = @LF Then $s_string = StringTrimRight($s_string, 1)
    If StringRight($s_string, 1) = @CR Then $s_string = StringTrimRight($s_string, 1)
    Global $h_open = FileOpen($s_outfile, 2)
    FileWrite($h_open, $s_string)
    FileClose($h_open)
    Sleep(3000)
    Global $s_string = FileRead($s_infile)
    $x = 1
;~ MsgBox(0, "String", $s_string)
    Do
        $s_string = StringRegExpReplace($s_string, "(\s\r\n){0,3}", "")
        Global $h_open = FileOpen($s_outfile, 2)
        FileWrite($h_open, $s_string)
        FileClose($h_open)
;~ MsgBox(0, "String", @Error)
        Global $s_string = FileRead($s_infile)
;~ MsgBox(0, "String", $s_string)
        $x += 1
    Until $x = 3
    Return 1
EndFunc   ;==>_BlankS
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...