Jump to content

How to Delete Everything to the right on a .txt line..?


Recommended Posts

I have a large hosts file that I'm trying to edit out all of the extra data out of that i got from here: http://www.mvps.org/winhelp2002/hosts.txt

what I need to do is search through the whole .txt file and find all the lines that have a "#" symbol in them and then delete everything to the right of the "#" symbol on that line including the "#" symbol itself..

How exactly could I do this..??

Link to comment
Share on other sites

I have a large hosts file that I'm trying to edit out all of the extra data out of that i got from here: http://www.mvps.org/winhelp2002/hosts.txt

what I need to do is search through the whole .txt file and find all the lines that have a "#" symbol in them and then delete everything to the right of the "#" symbol on that line including the "#" symbol itself..

How exactly could I do this..??

StringRegExpReplace()

:D

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

im a little drunk right now so bear with me...you could use string split and split each line at the # symbol (example follows) then rewrite the result to a new text file

Func _StringSplit ($sFilePath)
     $hFile = FileOpen($sFilePath, 0)
     $aStr = StringSplit(FileReadLine($hFile, $n), "#")
     Return $aStr
EndFunc

Thats just a very basic answer... It will return an array. $aStr[2] will contain everything to the right of the # and $aStr[1] will contain everything to the left of the #

Edited by LurchMan

Dating a girl is just like writing software. Everything's going to work just fine in the testing lab (dating), but as soon as you have contract with a customer (marriage), then your program (life) is going to be facing new situations you never expected. You'll be forced to patch the code (admit you're wrong) and then the code (wife) will just end up all bloated and unmaintainable in the end.

Link to comment
Share on other sites

You can probably use the following function:

#include<File.au3>

File_Delete_Com_Func ("C:\...\MyFile.txt")

Func File_Delete_Com_Func ($FilePath)
    $FileHndl=FileOpen($FilePath,0)

    If $FileHndl = -1 Then
        MsgBox(64,"Error!!!", "Unable to open the specified file")
    EndIf

    $linenum=1
    While $linenum
        $line=FileReadLine($FileHndl)
        if @error = -1 Then ExitLoop
        $repline=StringSplit($line,"#")
        _FileWriteToLine($FilePath,$linenum,$repline[1],1)
        $linenum = $linenum + 1
    WEnd

    FileClose ($FileHndl)
EndFunc
Link to comment
Share on other sites

is there any alternative to _FileWriteToLine, it seems to take too long to work through a large .txt file

and it doesn't seem to end properly for some odd reason.. my cpu goes up to 50% or more and stays like that until i kill the autoit PID

but i checked the .txt file and it seem to have gone through most of it but it didn't seem like it was working properly though..??

Link to comment
Share on other sites

is there any alternative to _FileWriteToLine, it seems to take too long to work through a large .txt file

and it doesn't seem to end properly for some odd reason.. my cpu goes up to 50% or more and stays like that until i kill the autoit PID

but i checked the .txt file and it seem to have gone through most of it but it didn't seem like it was working properly though..??

Try this one. If it does not work, it must be close.

;
Local $sFileName = "c:\FullPath\hosts.txt"

Local $aTmp = StringRegExpReplace(StringRegExpReplace(FileRead($sFileName), '(#\V*#\v*)|(#\V*)', ""), '(?m)(\v+)$', @CR);

ConsoleWrite(($aTmp) & @CRLF)
;#cs
$file = FileOpen($sFileName, 2)

; Check if file opened for writing OK
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

FileWrite($file, $aTmp)

FileClose($file)
;#ce
;
Link to comment
Share on other sites

THANX Malkey..

your code seems to work very well.. :D

A few more things I would like to do with this host file is remove all the 127.0.0.1 entries, make each URL into a "Base 2nd level Domain Ex: (noscript.net) (autoitscript.com) Ect..

then find all the duplicate URL's & remove all but one of them..

what all else would i need to add into the code above to do these extra functions..??

Link to comment
Share on other sites

what would I need to add into this line:

Local $aTmp = StringRegExpReplace(StringRegExpReplace(FileRead($sFileName), '(#\V*#\v*)|(#\V*)', ""), '(?m)(\v+)$', @CR);

to replace all the "www." & all the "www#." with "" in the hosts.txt file..??

Link to comment
Share on other sites

what would I need to add into this line:

Local $aTmp = StringRegExpReplace(StringRegExpReplace(FileRead($sFileName), '(#\V*#\v*)|(#\V*)', ""), '(?m)(\v+)$', @CR);

to replace all the "www." & all the "www#." with "" in the hosts.txt file..??

nothing, do another stringreplace
Link to comment
Share on other sites

How would i get rid of some string like this "www2."

with this (www\d) i can get rid of all stings with (www#)

but how can I get rid of the period "." as well too..??

because when i put "." in there it interprets it as "match any single character" after the digit and some characters wind up getting removed that i dont want removed..??

is there a pattern expression that represents a period "." ??

Link to comment
Share on other sites

How would i get rid of some string like this "www2."

with this (www\d) i can get rid of all stings with (www#)

but how can I get rid of the period "." as well too..??

because when i put "." in there it interprets it as "match any single character" after the digit and some characters wind up getting removed that i dont want removed..??

is there a pattern expression that represents a period "." ??

you have to escape the period

\.

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

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