Jump to content
Sign in to follow this  
BatMan22

Remove / at the end of every line in a file

Recommended Posts

BatMan22

So I have a text file with the / symbol at the end of %80 of the lines. I've been able to put together a few fixes already and customized my own 'find and replace' function from stuff I've found on the forums, but am stuck on this last replace.. Basically I need to be able to look at every line and ONLY if  /  is in the last character , then delete the /

What my file lines look like AFTER I run my script (just want to replace the last "/" symbols at the end of some of the lines) :

1711848-002A 300.1_W NO3/NO2/
1711855-001C 300.1_W NO3/NO2/CL/
1711849-001B 300.1_W NO3/NO2

What I want them to look like:

1711848-002A 300.1_W NO3/NO2
1711855-001C 300.1_W NO3/NO2/CL
1711849-001B 300.1_W NO3/NO2

$sFileName = "\\poop\IC3.txt" ; FileToWorkOn
FindAndReplace("<", "", $sFileName); Change First Two Patterns to go go!
FindAndReplace(">", "", $sFileName); Change First Two Patterns to go go!
FindAndReplace("; ", "/", $sFileName); Change First Two Patterns to go go!
FindAndReplace("NO3/NO3_1/", "NO3/", $sFileName); Change First Two Patterns to go go!
FindAndReplace("NO2/NO2_1/", "NO2/", $sFileName); Change First Two Patterns to go go!


;FunctionToFindAndReplaceCrap
Func FindAndReplace($sFind, $sReplace, $sFileName)

Local $iRetval = _ReplaceStringInFile($sFileName, $sFind, $sReplace)

If $iRetval = -1 Then
    MsgBox($MB_SYSTEMMODAL, "ERROR", "The pattern could not be replaced in file: " & $sFileName & " Error: " & @error)
    Exit
Else
    MsgBox($MB_SYSTEMMODAL, "INFO", "Found " & $iRetval & " occurances of the pattern: " & $sFind & " in the file: " & $sFileName)
EndIf

EndFunc

 

Edited by BatMan22
#hidetheguilty

Share this post


Link to post
Share on other sites
Malkey

The RE pattern means when there is a "/" at the end of a line then replace the "/" character with "",  nothing (delete it).

$sFileName = "\\poop\IC3.txt" ; FileToWorkOn

MsgBox(0, "", StringRegExpReplace(FileRead($sFileName), "(?m)/*$", "")) ; See StringRegExp() in AutoIt help to lookup meaning of regular expression's pattern.

 

  • Like 1

Share this post


Link to post
Share on other sites
BatMan22

While I appreciate the help.. Can you point me to the help page for that? Like.. how was I supposed to figure that out? I hate getting stuck >.< but appreciate your help immensely.

I'm an idiot, just read your ; part of you comment.

Edited by BatMan22

Share this post


Link to post
Share on other sites
BatMan22

So again with forum help.. This is what I currently have... My new problem is that for some reason this generates two random numbers as the top of the written file, I'm guessing it has something to do with the number of lines in the array, but how do I fix this?

Func ProcessAndCleanFiles()

;Remove the extra /'s at the end of each TestCodeLineFor IC3
$Whattowrite= StringRegExpReplace(FileRead($sFileName), "(?m)/*$", "")
FileOpen($sFileName, 2)
FileWrite($sFilename, $Whattowrite)

Local $aFile, $aArray
_FileReadToArray($sFileName, $aFile)
_ArrayDisplay($aFile)
$aArray = _ArrayUnique($aFile)
_ArrayDelete($aArray, 0)
_ArrayDisplay($aArray)
_FileWriteFromArray ($sFileName, $aArray)

EndFunc

 

Share this post


Link to post
Share on other sites
Malkey

Hello again.

Both functions, _FileReadToArray() and _ArrayUnique(), have parameters that enable or disable the line count being assigned to the first element of the array- see AutoIt help file.

Or you can click on these functions that are blue in colour in the code box below to see the online AutoIt help.

#include <File.au3>
#include <Array.au3>

ProcessAndCleanFiles()

Func ProcessAndCleanFiles()
    Local $sFileName = "\\poop\IC3.txt" ; FileToWorkOn
    ;Remove the extra /'s at the end of each TestCodeLineFor IC3
    Local $Whattowrite = StringRegExpReplace(FileRead($sFileName), "(?m)/*$", "")
    Local $hFileOpen = FileOpen($sFileName, 2)
    FileWrite($hFileOpen, $Whattowrite)
    FileClose($hFileOpen)

    ; Remove duplicates from file
    Local $aFile, $aArray
    _FileReadToArray($sFileName, $aFile, 0) ; $iFlags parameter, $FRTA_NOCOUNT (0) - array is 0-based use UBound() to get size,
    _ArrayDisplay($aFile)

    $aArray = _ArrayUnique($aFile, 0, 0, 0, 0) ; $iCount parameter, $ARRAYUNIQUE_NOCOUNT (0) returns a list without a count.
    _ArrayDisplay($aArray)
    _FileWriteFromArray($sFileName, $aArray)

EndFunc   ;==>ProcessAndCleanFiles

 

Edited by Malkey
  • Like 1

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  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.