Sign in to follow this  
Followers 0
brNwsH

Edit text lines in a .txt file

5 posts in this topic

#1 ·  Posted (edited)

Hey Com,

im new here and i need some help.

i searched the forums already and didnt found anything.

i have a text file with some textlines.

some of these lines have an [xyz] infront of the text.

is it possible to search these lines and only delete the clamp + text in between them?

thx

brN

//EDIT

i think i got it

can be closed

Edited by brNwsH

Share this post


Link to post
Share on other sites



Welcome to the forums ! Posted Image

Try

#include <String.au3>

$_FilePath = 'C:\text.txt'
$_FileRead = FileRead ( $_FilePath )
$_NewText = _RemoveBetweenBrackets ( $_FileRead, 1 )
ConsoleWrite ( "!->-- $_NewText : " & $_NewText & @Crlf )
$_file = FileOpen ( $_FilePath, 2 )
FileWrite ( $_file, $_NewText )
FileClose ( $_file )

Func _RemoveBetweenBrackets ( $_Stringg, $_Option=1 ) ; 1 [ ], 2 ( ), 3 { }, 4 < >, 5 ‹ ›, 6 « »
    Local $_BracketsArray[13] = [12, '[' ,']' ,'(' ,')' ,'{', '}', '<', '>', '‹', '›', '«', '»']
    Local $_chr1 = $_BracketsArray[$_Option*2-1], $_chr2 = $_BracketsArray[$_Option*2]
    $_StringBetweenArray = _StringBetween ( $_Stringg, $_chr1, $_chr2 )
    If Not @error Then
        For $_S = 0 To UBound ( $_StringBetweenArray ) -1
            $_StringInStr1 = StringInStr ( $_Stringg, $_chr1 & $_StringBetweenArray[$_S] & $_chr2 ) -1
            $_StringInStr2 = StringLen ( $_StringBetweenArray[$_S] ) + 2
            $_Stringg = StringLeft ( $_Stringg, $_StringInStr1 )  & StringRight ( $_Stringg, StringLen ( $_Stringg ) - $_StringInStr1-$_StringInStr2 )
        Next
    Else
        Return $_Stringg
    EndIf
    Return StringStripWS ( $_Stringg, 7 )
EndFunc ;==> _RemoveBetweenBrackets ( )

It remove all occurences between brackets.


AutoIt 3.3.14.2 X86 - SciTE 3.6.0WIN 8.1 X64 - Other Example Scripts

Share this post


Link to post
Share on other sites

Welcome to the forums ! Posted Image

Try

#include <String.au3>

$_FilePath = 'C:\text.txt'
$_FileRead = FileRead ( $_FilePath )
$_NewText = _RemoveBetweenBrackets ( $_FileRead, 1 )
ConsoleWrite ( "!->-- $_NewText : " & $_NewText & @Crlf )
$_file = FileOpen ( $_FilePath, 2 )
FileWrite ( $_file, $_NewText )
FileClose ( $_file )

Func _RemoveBetweenBrackets ( $_Stringg, $_Option=1 ) ; 1 [ ], 2 ( ), 3 { }, 4 < >, 5 ‹ ›, 6 « »
    Local $_BracketsArray[13] = [12, '[' ,']' ,'(' ,')' ,'{', '}', '<', '>', '‹', '›', '«', '»']
    Local $_chr1 = $_BracketsArray[$_Option*2-1], $_chr2 = $_BracketsArray[$_Option*2]
    $_StringBetweenArray = _StringBetween ( $_Stringg, $_chr1, $_chr2 )
    If Not @error Then
        For $_S = 0 To UBound ( $_StringBetweenArray ) -1
            $_StringInStr1 = StringInStr ( $_Stringg, $_chr1 & $_StringBetweenArray[$_S] & $_chr2 ) -1
            $_StringInStr2 = StringLen ( $_StringBetweenArray[$_S] ) + 2
            $_Stringg = StringLeft ( $_Stringg, $_StringInStr1 )  & StringRight ( $_Stringg, StringLen ( $_Stringg ) - $_StringInStr1-$_StringInStr2 )
        Next
    Else
        Return $_Stringg
    EndIf
    Return StringStripWS ( $_Stringg, 7 )
EndFunc ;==> _RemoveBetweenBrackets ( )

It remove all occurences between brackets.

Hey thx it works good but it is writing everything successively.

e.g.

[abc]abc

[def]def

[ghi]ghi

________solution

abcdefghi

but i want it underneath

abc

def

ghi

thx

brN

Share this post


Link to post
Share on other sites

With a text file containing this

[abc]abc

[def]def

[ghi]ghi

function return this :

abc

def

ghi

and not abcdefghi ! Posted Image

#include <String.au3>

$_FilePath = 'C:\text.txt'
$_FileRead = FileRead ( $_FilePath )
$_NewText = _RemoveBetweenBrackets ( $_FileRead, 1 )
ConsoleWrite ( "!->-- $_NewText : " & $_NewText & @Crlf )
$_file = FileOpen ( $_FilePath, 2 )
FileWrite ( $_file, $_NewText )
FileClose ( $_file )
Run ( 'notepad ' & $_FilePath )

Func _RemoveBetweenBrackets ( $_Stringg, $_Option=1 ) ; 1 [ ], 2 ( ), 3 { }, 4 < >, 5 ‹ ›, 6 « »
    Local $_BracketsArray[13] = [12, '[' ,']' ,'(' ,')' ,'{', '}', '<', '>', '‹', '›', '«', '»']
    Local $_chr1 = $_BracketsArray[$_Option*2-1], $_chr2 = $_BracketsArray[$_Option*2]
    $_StringBetweenArray = _StringBetween ( $_Stringg, $_chr1, $_chr2 )
    If Not @error Then
        For $_S = 0 To UBound ( $_StringBetweenArray ) -1
            $_StringInStr1 = StringInStr ( $_Stringg, $_chr1 & $_StringBetweenArray[$_S] & $_chr2 ) -1
            $_StringInStr2 = StringLen ( $_StringBetweenArray[$_S] ) + 2
            $_Stringg = StringLeft ( $_Stringg, $_StringInStr1 )  & StringRight ( $_Stringg, StringLen ( $_Stringg ) - $_StringInStr1-$_StringInStr2 )
        Next
    Else
        Return $_Stringg
    EndIf
    Return StringStripWS ( $_Stringg, 7 )
EndFunc ;==> _RemoveBetweenBrackets ( )

AutoIt 3.3.14.2 X86 - SciTE 3.6.0WIN 8.1 X64 - Other Example Scripts

Share this post


Link to post
Share on other sites

yeah the function is returning right

but in my txt file everything is writtin successively

so far

brN

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