Sign in to follow this  
Followers 0
michaelslamet

[SOLVED] Remove CRLF at the end of text file

15 posts in this topic

#1 ·  Posted (edited)

Hi :-)

What is the best way to remove the CRLF at the end of text file? Is there any build-in function to do it?

For example, my text file content:

Line1

Line2

When I open it with notepad and press Ctrl+End, the cursor will placed on Line3 Col 1, I wish to remove that CRLF at line 3 so when I press Ctrl+End on notepad,

the cursor will be at Col 6 at Line 2.

Thanks :-)

Edited by michaelslamet

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

michaelslamet,

This is one way to do it.

* - garbage code replace by below

kylomas

edit: code modified to check for @CRLF before replacing

local $fl_string = fileread(@scriptdir & '\test.txt')
if stringright($fl_string,2) = @crlf then $fl_string = stringtrimright($fl_string,2)
filedelete(@scriptdir & '\test.txt')
filewrite(@scriptdir & '\test.txt',$fl_string)
Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

Or this too...

ConsoleWrite(_StringLastCRLF(FileRead(@ScriptFullPath)) & @CRLF)

Func _StringLastCRLF($sString)
    Return StringRegExpReplace($sString, '\v{1,2}$', '')
EndFunc   ;==>_StringLastCRLF

_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 04/09/2015

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

;KeyPoint : Removes the CRLF after which no character is present

$sText = 'Line1' & @CRLF & 'Line2' & @CRLF
$sRegEx = StringRegExpReplace($sText, '(?m)([\r\n]*)$', '')
ConsoleWrite($sRegEx & @CR)

@guiness,

[Out of curiosity]

Are Carriage Return and Line Feed the only vertical white space characters ?

Edited by PhoenixXL

My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

Share this post


Link to post
Share on other sites

That's the only two I can think of, but there might be more. What was wrong with my version out of curiosity?


_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 04/09/2015

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

If more than two "vertical space" there it will not work. For Word-Wrapping two Carriage Returns are used.

Though it wouldnt make any difference till EOF is prior

Edited by PhoenixXL

My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

Share this post


Link to post
Share on other sites

Fair point.


_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 04/09/2015

Share this post


Link to post
Share on other sites

@ALL,

This code builds a table of which regexp shortcut symbol matches each ascii character. You can use this to answer the question in post#4.

(Code built from a sample provided by Robjong)

#include<array.au3>

; regexp pattern tbl and char tbl/ one column for each regexp pattern

local $aRegexp[20] = [' ',' ','\a','\e','\f','\h','\n','\r','\s','\t','\v','\w','\V','\H','\S','\d','\D']
local $aCharTBL[256][ubound($aRegexp)]

; build tbl of ascii chars and regexp patterns that match

For $i = 0 To ubound($aCharTBL) - 1
    $aCharTBL[$i][0] = hex($i,3)
    $aCharTBL[$i][1] = chr($i)
    for $j = 2 to ubound($aRegexp) - 1
        If StringRegExp(chr($i), $aRegexp[$j]) Then
            $aCharTBL[$i][$j] = $aRegexp[$j]
        endif
    next
next

; count how many times each pattern matched to re-arrange tbl

local $acolcnt[ubound($aCharTBL,2)][2], $cnt
for $i = 2 to ubound($aCharTBL,2) - 1
    $cnt = 0
    for $j = 0 to ubound($aCharTBL,1) - 1
        if $aCharTBL[$j][$i] <> '' then $cnt += 1
    Next
    $acolcnt[$i][0] = $cnt
    $acolcnt[$i][1] = $i

Next

_arraysort($acolcnt,1)

; re-create regexp pattern tbl in matched order, most to least

local $aSRTDRegExp[20]
for $i= 0 to ubound($aSRTDRegExp) - 3
    $aSRTDRegExp[$i+2] = $aRegexp[$acolcnt[$i][1]]
Next
$aRegexp = $aSRTDRegExp

; blank origional array and re-populate in most to least order

$aCharTBL = 0
local $aCharTBL[256][ubound($aRegexp)]

For $i = 0 To ubound($aCharTBL) - 1
    $aCharTBL[$i][0] = hex($i,3)
    $aCharTBL[$i][1] = chr($i)
    for $j = 2 to ubound($aRegexp) - 1
        If StringRegExp(chr($i), $aRegexp[$j]) Then
            $aCharTBL[$i][$j] = $aRegexp[$j]
        endif
    next
next

; replace 0 - 31 with nemonics

$s = 'NUL,SOH,STX,ETX,EOT,ENQ,ACK,BEL,BS,HT,LF,VT,FF,CR,SO,SI,DLE,DC1,DC2,DC3,DC4,NAK,SYN,ETB,CAN,EM,SUB,ESC,FS,GS,RS,US'

$a = stringsplit($s,',',2)

for $i = 0 to ubound($a) - 1
    $aCharTBL[$i][1] = $a[$i]
next

_arraydisplay($aCharTBL)

kylomas


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

To answer PhoenixXL question, from the PCRE docs:

The vertical spacing codepoints are:

U+000A Linefeed

U+000B Vertical tab

U+000C Formfeed

U+000D Carriage return

U+0085 Next line

U+2028 Line separator

U+2029 Paragraph separator

Note that the last two are always considered vertical spacing for the v and V metacharacters regardless whether the PCRE library is compiled with the --enable-unicode-properties option (which is halas not used in the current builds on the peculiar ground that it inflates the library size by 30k).

The situation for horizontal spacing codepoints is similar (for h and H):

U+0009 Horizontal tab

U+0020 Space

U+00A0 Non-break space

U+1680 Ogham space mark

U+180E Mongolian vowel separator

U+2000 En quad

U+2001 Em quad

U+2002 En space

U+2003 Em space

U+2004 Three-per-em space

U+2005 Four-per-em space

U+2006 Six-per-em space

U+2007 Figure space

U+2008 Punctuation space

U+2009 Thin space

U+200A Hair space

U+202F Narrow no-break space

U+205F Medium mathematical space

U+3000 Ideographic space

Refer to official PCRE docs for details.


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

Geez, and I thought I had a good answer...Merry Christmas JCHD!


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

Here a different approach:

RemoveLastCRLFsFromFile(@ScriptDir & "\Test.txt", @ScriptDir & "\Test_New.txt")

Func RemoveLastCRLFsFromFile($sFile, $sNewFile) ;coded by UEZ 2012
    Local $hFile = FileOpen($sFile, 16)
    If @error Then Return SetError(1, 0, 0)
    Local $binText = FileRead($hFile)
    FileClose($hFile)
    Local $iPos = 0, $iVal
    For $i = BinaryLen($binText) To 1 Step - 1
        $iVal = Int(BinaryMid($binText, $i, 1))
        If $iVal = 0x0A  Or $iVal = 0x0D Then
            $iPos += 1
        Else
            ExitLoop
        EndIf
    Next
    If Not $iPos Then Return SetError(2, 0, 0) ;no CR/LF found
    $hFile = FileOpen($sNewFile, 18)
    If @error Then Return SetError(3, 0, 0)
    FileWrite($hFile, BinaryMid($binText, 1, BinaryLen($binText) - $iPos))
    FileClose($hFile)
    Return 1
EndFunc

Br,

UEZ

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites

#12 ·  Posted (edited)

@jchd,

Thanks for the answer :guitar:

@kylomas

Thanks for the effort

Edited by PhoenixXL

My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

Share this post


Link to post
Share on other sites

PhoenixXL,

Thanks, that's pretty much what I tell my sons after they put the rear brake shoes on backwards :rolleyes: Merry XMAS!!

Run the code that I posted, you might find it interesting. I wrote that when I was trying to figure out what the hell "whitespace" is.

kylomas


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

Run the code that I posted, you might find it interesting.

Already did that ;)

It explains 'bout the characters pretty well

EXCELLENT EFFORT


My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

Share this post


Link to post
Share on other sites

Thank you very much for everybody for the solutions :)

I learn alot from this forum.

Merry Christmas and Happy New Year!

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