Jump to content

Want to delete some characters in a textfile


Recommended Posts

:party Hi there,

i have the following textfile:

Suchprofil= [$ic=c02]

Datenbank=d:\cds\USP2005145 | Dokument=US 50072186 A1 20050407 | Nummer=281

Datenbank=d:\cds\USP2005146 | Dokument=US 50072720 A1 20050407 | Nummer=65

Datenbank=d:\cds\USP2005146 | Dokument=US 50072722 A1 20050407 | Nummer=67

Datenbank=d:\cds\USP2005146 | Dokument=US 50072732 A1 20050407 | Nummer=77

Datenbank=d:\cds\USP2005146 | Dokument=US 50072739 A1 20050407 | Nummer=84

Now i want to delete the "| Nummer=xx" characters in each line where the numbers

are different so stringreplace did not work here?

i tried it liek this:

$text55=StringReplace($text4,"| Nummer=","xxxxxxxxxxxxxxxxxxxxxxxxx ")

filewrite .....

But of course onle the "|nummer=" would be replaced.

Has anyone a tip for me?

Thanks

Greetings

Mike

: :evil::D:D:)

Link to comment
Share on other sites

Not exactly the cleanest way to do it, but..

$file = FileOpen("test.txt", 0)
$new_file = FileOpen("newtest.txt", 1)


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

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

; Read in lines of text until the EOF is reached
While 1

$line = FileReadLine($file)
If @error = -1 Then ExitLoop

$split_line = StringSplit($line, "|")
if ($split_line[0] == 3) then
; Since line of code we want to replace always has 3 delimiters in it
; Datenbank=..6 | Dokument=U..07 | Nummer=65 
; this must be one of those lines in the text

; Rebuild the line the way we want it, leaving out third element in the array
$new_line = $split_line[1] & "|" & $split_line[2];
$line = $new_line;
else
; Not code we have to replace
endIf

FileWriteLine($new_file, $line)
Wend

FileClose($file)
FileClose($new_file)
Edited by sammysnake
Link to comment
Share on other sites

...another way would be to replace the "split/rebuild" part of sammysnake's code to:

$line = StringLeft($line, (StringInStr($line, "|", 1, 2) - 1))

(other minor changes to sammysnake's code)

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

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

; Read in lines of text until the EOF is reached
While 1
    $line = FileReadLine($file)
    If @error = -1 Then ExitLoop
    $line = StringLeft($line, (StringInStr($line, "|", 1, 2) - 1))
    FileWriteLine($new_file, $line)
WEnd

FileClose($file)
FileClose($new_file)

The code above duplicates the results of sammysnake's code.

I think that both methods will give you *exactly* what you asked for:

[...delete the "| Nummer=xx" characters ...]

However, you might want to change the "- 1" to "- 2" in this part or the code above

(StringInStr($line, "|", 1, 2) - 1)

if you want to remove the last space on each line...

"- 1 " deletes the "| Nummer=xx" characters

"- 2 " deletes the " | Nummer=xx" characters

enjoy

[size="1"][font="Arial"].[u].[/u][/font][/size]

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