Skip to content
⌘ NSIS Forum Archive

find a word in unicode file and remove that line

27 posts

r2du-soft#

find a word in unicode file and remove that line

hi
for example i have a unicode file with this texts:

hi
hello
my car
cat

i want find word "my car" number of line and remove that line:

hi
hello
cat

how can do this?
thanks
Anders#
Please don't bump your thread after just one day, it is very rude!

You have not provided a lot of info (NSIS version? Unicode installer?) nor any examples of things you have tried.

If this is a Unicode installer you could use FileReadUTF16LE and write the lines you want to keep to another file and then replace the original file when you are done...
r2du-soft#
Originally Posted by stass View Post
r2du-soft , use :

${LineRead}
${WordFind}
${LineFind}
im try but i cant...


Originally Posted by Anders View Post
Please don't bump your thread after just one day, it is very rude!

You have not provided a lot of info (NSIS version? Unicode installer?) nor any examples of things you have tried.

If this is a Unicode installer you could use FileReadUTF16LE and write the lines you want to keep to another file and then replace the original file when you are done...
im sorry for send a post again on one day mr Anders.
but i very need to quick help

my text file format is unicode,i want search a word in that file and if find that word in that text file i remove line(row) where the word is located.
stass#
OutFile "WordFind-test.exe"
!include "TextFunc.nsh"
!include "WordFunc.nsh"
!include "StrFunc.nsh"
${StrRep}
!define MyFile "$EXEDIR\test.txt"
Var FindInFile
Var LineReplace
Var LineToInsert 
Var CurLineNumber
Var CurLine
Var LineNumbers 
Section
StrCpy $FindInFile ${MyFile}
StrCpy $LineReplace 'my car' ; Search and replace the specified phrase
StrCpy $LineToInsert '' ; replacement to ...
call ReplaceLinesInFile
SectionEnd
Function ReplaceLinesInFile
${LineSum} $FindInFile $LineNumbers ; Get sum of lines in text file * http://nsis.sourceforge.net/LineSum
strcpy $CurLineNumber 0
startLineRead:
intop $CurLineNumber $CurLineNumber + 1
IntCmp $CurLineNumber $LineNumbers 0 0 endFunc ; If the current line number is greater than the number of lines in the file - the search is completed
${LineRead} "$FindInFile" "$CurLineNumber" $CurLine ; Get line in file specified with number * http://nsis.sourceforge.net/LineRead
${WordFind} '$CurLine' "$LineReplace" "E+1{" $R0 ; Finding text in a row * http://nsis.sourceforge.net/WordFind
StrCmp $R0 "1" startLineRead 0
${LineFind} "$FindInFile" "" "" "Insert_Line" ; Find specified lines in text file and edit or view this lines in callback function.* http://nsis.sourceforge.net/LineFind
goto startLineRead
; If you need a replacement of only one value - remove the line above
endFunc:
FunctionEnd
Function Insert_Line
StrCmp $R8 "$CurLineNumber" 0 push
${StrRep} $CurLine "$CurLine" "$LineReplace" "$LineToInsert"
FileWrite $R4 "$CurLine"
StrCpy $0 SkipWrite
push:
Push $0
FunctionEnd 
r2du-soft#
Originally Posted by stass View Post
OutFile "WordFind-test.exe"
!include "TextFunc.nsh"
!include "WordFunc.nsh"
!include "StrFunc.nsh"
${StrRep}
!define MyFile "$EXEDIR\test.txt"
Var FindInFile
Var LineReplace
Var LineToInsert 
Var CurLineNumber
Var CurLine
Var LineNumbers 
Section
StrCpy $FindInFile ${MyFile}
StrCpy $LineReplace 'my car' ; Search and replace the specified phrase
StrCpy $LineToInsert '' ; replacement to ...
call ReplaceLinesInFile
SectionEnd
Function ReplaceLinesInFile
${LineSum} $FindInFile $LineNumbers ; Get sum of lines in text file * http://nsis.sourceforge.net/LineSum
strcpy $CurLineNumber 0
startLineRead:
intop $CurLineNumber $CurLineNumber + 1
IntCmp $CurLineNumber $LineNumbers 0 0 endFunc ; If the current line number is greater than the number of lines in the file - the search is completed
${LineRead} "$FindInFile" "$CurLineNumber" $CurLine ; Get line in file specified with number * http://nsis.sourceforge.net/LineRead
${WordFind} '$CurLine' "$LineReplace" "E+1{" $R0 ; Finding text in a row * http://nsis.sourceforge.net/WordFind
StrCmp $R0 "1" startLineRead 0
${LineFind} "$FindInFile" "" "" "Insert_Line" ; Find specified lines in text file and edit or view this lines in callback function.* http://nsis.sourceforge.net/LineFind
goto startLineRead
; If you need a replacement of only one value - remove the line above
endFunc:
FunctionEnd
Function Insert_Line
StrCmp $R8 "$CurLineNumber" 0 push
${StrRep} $CurLine "$CurLine" "$LineReplace" "$LineToInsert"
FileWrite $R4 "$CurLine"
StrCpy $0 SkipWrite
push:
Push $0
FunctionEnd 
thanks stass
but when use on unicode file program abort!
i want export from registry,after that in that registry file is a line with this text:
"Password"=hex:0e,0e,f,00,43,05,43,00.43,43,43,43,43,00,9c,b5,
i want find "Password" in what line and remove that line completely..
download and see this file:

i want find password line in this file and remove password line...
stass#
r2du-soft

You're all confused! It turns out you need - search hex values of the registry key. It is quite another matter.

Although it is not clear ... Again - need to remove the line in the file registry or in the registry?

Use http://nsis.sourceforge.net/Registry_plug-in
r2du-soft#
Originally Posted by stass View Post
r2du-soft

You're all confused! It turns out you need - search hex values of the registry key. It is quite another matter.

Although it is not clear ... Again - need to remove the line in the file registry or in the registry?

Use http://nsis.sourceforge.net/Registry_plug-in
im sorry,my English is bad
1-i export a registry key with this code:
ExecWait 'regedit.exe /e "c:\test.reg" HKEY_LOCAL_MACHINE\SOFTWARE\COWON'
now test.reg format is unicode!
in test.reg is a line with this text:
"Password"=hex:0e,0e,f,00,43,05,43,00.43,43,43,43,43,00,9c,b5,
in all computer "password" is steady but =hex:0e,0e,f,00,43,05,43,00.43,43,43,43,43,00,9c,b5, change
i want find linear where in word password is located and remove that line.

for example think i have in test.reg:

my word1
"Password"=hex:0e,0e,f,00,43,05,43,00.43,43,43,43,43,00,9c,b5,
my word2

i want remove Password line and after that i have in test.reg:
my word1
my word2
stass#
r2du-soft
test.reg format is unicode!
Use http://nsis.sourceforge.net/Unicode_FileRead

outfile FileReadUnicode.exe
Section
FileOpen $R0 "$EXEDIR\test.txt" r
    IfErrors done
read:    
    Push $R0
    Call FileReadUnicode
    Pop $R1
    IfErrors done
    StrCpy $R2 "$R2 $R1"
    Goto read
done:
;MessageBox MB_OK "$R2$\r$\n" 
SectionEnd
Function "FileReadUnicode"
    Push $0
    Exch 
    Pop $0
    Push $1
    Push $2
    Push $3
    Push $4
 
    IntOp $4 0 + 0
    IntOp $3 0 + 0
    StrCpy $1 ""
    SetErrorLevel 0
read:    
    FileReadByte $0 $2
    IntCmp $4 0 0 skip skip
    IntCmp $2 0x0a skip 0 0 
    IntCmp $2 0x0d readsecondhalf 0 0 
    IntCmp $2 0 done 0 0
    IntFmt $2 "%c" $2
    StrCpy $1 "$1$2"
skip:    
    IntOp $4 $4 + 1
    IntOp $4 $4 % 2
    Goto read
readsecondhalf:    
    FileReadByte $0 $2
done:    
    Pop $4
    Pop $3
    Pop $2
    Push $1
    Exch 
    Pop $1
    Exch 
    Pop $0
FunctionEnd 
to $R2 - the contents of the file
Then search for the line
r2du-soft#
Originally Posted by stass View Post
thanks stass
that NewTextreplace good work in unicode,easily find worr and easily replace and easily save file,but how can see the word found Located at in Which line?
stass#
line number :

OutFile "FileReadUnicode_line number.exe"
Section
FileOpen $R0 "$EXEDIR\test.txt" r
    IfErrors done
loop:    
    Push $R0
    Call FileReadUnicode
    Pop $R1
    IfErrors done
    
    IntOp $R3 $R3 + 1
    StrCmp $R1 '"Password"=hex:0e,0e,f,00,43,05,43,00.43,43,43,43,43,00,9c,b5,' done
    Goto loop
done:
MessageBox MB_OK "$R1$\r$\nline number: $R3" 
SectionEnd
Function "FileReadUnicode"
    Push $0
    Exch 
    Pop $0
    Push $1
    Push $2
    Push $3
    Push $4
 
    IntOp $4 0 + 0
    IntOp $3 0 + 0
    StrCpy $1 ""
    SetErrorLevel 0
loop:    
    FileReadByte $0 $2
    IntCmp $4 0 0 skip skip
    IntCmp $2 0x0a skip 0 0 
    IntCmp $2 0x0d readsecondhalf 0 0 
    IntCmp $2 0 done 0 0
    IntFmt $2 "%c" $2
    StrCpy $1 "$1$2"
skip:    
    IntOp $4 $4 + 1
    IntOp $4 $4 % 2
    Goto loop
readsecondhalf:    
    FileReadByte $0 $2
done:    
    Pop $4
    Pop $3
    Pop $2
    Push $1
    Exch 
    Pop $1
    Exch 
    Pop $0
FunctionEnd 
r2du-soft#
Originally Posted by stass View Post
line number :

OutFile "FileReadUnicode_line number.exe"
Section
FileOpen $R0 "$EXEDIR\test.txt" r
    IfErrors done
loop:    
    Push $R0
    Call FileReadUnicode
    Pop $R1
    IfErrors done
    
    IntOp $R3 $R3 + 1
    StrCmp $R1 '"Password"=hex:0e,0e,f,00,43,05,43,00.43,43,43,43,43,00,9c,b5,' done
    Goto loop
.
.
.
.
. 


thanks stass but not work!
please download attachment file,this is my unicode .text file,download and test on that...
now i just want find password line number
r2du-soft#
Originally Posted by stass View Post
With your file - it works fine.
In my script, at the end of the line - comma ( b5, )
mr stass comma (b5) work me in ansi format,not work in unicode and also that code just replace 'mycar' with line...
Suppose i have a unicde text file with under content:

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\NSIS]
@="C:\\Program Files\\NSIS"
"VersionMajor"=dword:00000003
"VersionMinor"=dword:00000000
"Password"=hex:0e,0e,f,00,43,05,43,00.43,43,43,43,43,00,9c,b5
"VersionRevision"=dword:00000004
"VersionBuild"=dword:00000000
i want serarch password word and find that Located in which line,in my example password word is in line 7
now i found number of line,now i want remove line 7 full
and after remove line 7 my file change to this

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\NSIS]
@="C:\\Program Files\\NSIS"
"VersionMajor"=dword:00000003
"VersionMinor"=dword:00000000
"VersionRevision"=dword:00000004
"VersionBuild"=dword:00000000

NewTextreplace good work in unicode file but with NewTextreplace just can replace word.
stass#
Remove the row: "Password"=hex:0e,0e,f,00,43,05,43,00.43,43,43,43,43,00,9c,b5'
in unicode file: test.txt (More correct - create a new target file - test2.txt )
OutFile "FileReadUnicode_remove_line_number.exe"
Section
FileOpen $R0 "$EXEDIR\test.txt" r   ; unicode file
FileOpen $R2 "$EXEDIR\test2.txt" w   ;  file in which the string is removed
    IfErrors done
loop:    
    Push $R0
    Call FileReadUnicode
    Pop $R1
    IfErrors done
    
IntOp $R3 $R3 + 1
StrCmp $R3 '1' 0 +2
StrCpy $R1 'Windows Registry Editor Version 5.00'
StrCmp $R1 '"Password"=hex:0e,0e,f,00,43,05,43,00.43,43,43,43,43,00,9c,b5' skip
ClearErrors
IfErrors done
FileWriteUTF16LE $R2 "$R1$\r$\n"
skip:
    Goto loop
done:
FileClose $R2
SectionEnd
Function "FileReadUnicode"
    Push $0
    Exch 
    Pop $0
    Push $1
    Push $2
    Push $3
    Push $4
 
    IntOp $4 0 + 0
    IntOp $3 0 + 0
    StrCpy $1 ""
    SetErrorLevel 0
loop:    
    FileReadByte $0 $2
    IntCmp $4 0 0 skip skip
    IntCmp $2 0x0a skip 0 0 
    IntCmp $2 0x0d readsecondhalf 0 0 
    IntCmp $2 0 done 0 0
    IntFmt $2 "%c" $2
    StrCpy $1 "$1$2"
skip:    
    IntOp $4 $4 + 1
    IntOp $4 $4 % 2
    Goto loop
readsecondhalf:    
    FileReadByte $0 $2
done:    
    Pop $4
    Pop $3
    Pop $2
    Push $1
    Exch 
    Pop $1
    Exch 
    Pop $0
FunctionEnd 
My code works (I use nsis-2.46.5-Unicode)
r2du-soft#
thanks
when compile i have a error
Error: FileWriteUTF16LE is only available when building a Unicode installer
Error in script "C:\Users\Rose\Desktop\5.nsi" on line 22 -- aborting creation process
line 22 is: FileWriteUTF16LE $R2 "$R1$\r$\n"

and another question:
is possible save Overwrite file?or must save with another name?
stass#
r2du-soft
Error: FileWriteUTF16LE is only available when building a Unicode installer
Use Unicode installer ( http://www.scratchpaper.com/ )
Filename - any (test.reg etc.)
Anders#
Originally Posted by stass View Post
r2du-soft

Use Unicode installer ( http://www.scratchpaper.com/ )
Filename - any (test.reg etc.)
I'm guessing he is already using NSIS 3 and just needs to add "Unicode true" to his .nsi
r2du-soft#
Originally Posted by Anders View Post
I'm guessing he is already using NSIS 3 and just needs to add "Unicode true" to his .nsi
yes mr Anders i use from nullsoft script install system 3.0b1,thanks i use from Unicode true and compile with nsis 3
now i have another question:
is possible save to test.txt after remove that line?no text2.txt?

and i have two problem:
1-i just need to "password" search and remove that line,in word:
"Password"=hex:0e,0e,f,00,43,05,43,00.43,43,43,43,43,00,9c,b5
password is a stable word in all computer but hex:0e,0e,f,00,43,05,43,00.43,43,43,43,43,00,9c,b5 is change!
i must just search password and remove that line....i cant find (havent) that hex,that hex in each computer changing...Search by password word.
1-in some registry file after remove password line with code #5 i see change other values is file!
like this:
in original unicode: test.txt = "PersianFarsiText"="این یک تست است"
after remove(replace) that line: test.txt = "PersianFarsiText"="'ÌF Ì© *3* '3*"


is note better code for do this works (find a word and show line number{that word in which line is located},remove that line by line number)?!
jpderuiter#
Come on, this is very basic stuff.
Replace
StrCmp $R1 '"Password"=hex:0e,0e,f,00,43,05,43,00.43,43,43,43,43,00,9c,b5' skip 
by
StrCpy $R2 $R1 11
StrCmp $R2 '"Password"=' skip
You cannot write to test.txt, because you have it already open for reading.
So write to test2.txt, delete test.txt and rename test2.txt to test.txt
Anders#
It would technically be possible to do it on a single file if you open it for appending but after you find the line to delete you have to switch to readbyte/writebyte and call setendoffile with the system plugin at the end.
r2du-soft#
Originally Posted by Anders View Post
It would technically be possible to do it on a single file if you open it for appending but after you find the line to delete you have to switch to readbyte/writebyte and call setendoffile with the system plugin at the end.
thanks mr Anders ,im try but cant

is possible convert this code to nsis code?

Dim intLine As Integer

Dim FF As Integer
Dim strTemp As String, strSearch As String

intLine = 0
FF = FreeFile

strSearch = Trim$(LCase$("Password"))

Open (Environ("HOMEDRIVE") & "\test.txt") For Input As #FF

  While Not EOF(FF)
    Line Input #FF, strTemp
    intLine = intLine + 1
    
    
    If InStr(1, LCase$(strTemp), strSearch) <> 0 Then

      Label2.Caption = "" & CStr(intLine)
              MsgBox ("Find World")
          
      Exit Sub
    End If

  Wend
Close #FF

i write this code by vb6,this code search word "password" in unicode text file and show number of line in a label...
Anders#
The line numbers in exported reg files are not stable so why do you care what the line number is? You should be able to figure out how to count it on your own if you really need this.

Posting VB code is not that helpful, try coding the NSIS version and we can help once you get started, you cannot expect people to write all the code for you...
r2du-soft#
im sorry masters i don't test true your code...your codes is true:

Section
FileOpen $R0 "$EXEDIR\test.txt" r
    IfErrors done
loop:    
    Push $R0
    Call FileReadUnicode
    Pop $R1
    IfErrors done

    IntOp $R3 $R3 + 1
    ;StrCmp $R1 '"Password"=hex:0e,00,43,00,f5,00,64,00,1d,00,28,00,9c,00,b5,00' done

	StrCpy $R2 $R1 11
	StrCmp $R2 '"Password"=' done

    Goto loop
done:
MessageBox MB_OK "$R1$\r$\nline number: $R3" 
SectionEnd

Function "FileReadUnicode"
    Push $0
    Exch 
    Pop $0
    Push $1
    Push $2
    Push $3
    Push $4
 
    IntOp $4 0 + 0
    IntOp $3 0 + 0
    StrCpy $1 ""
    SetErrorLevel 0
loop:    
    FileReadByte $0 $2
    IntCmp $4 0 0 skip skip
    IntCmp $2 0x0a skip 0 0 
    IntCmp $2 0x0d readsecondhalf 0 0 
    IntCmp $2 0 done 0 0
    IntFmt $2 "%c" $2
    StrCpy $1 "$1$2"
skip:    
    IntOp $4 $4 + 1
    IntOp $4 $4 % 2
    Goto loop
readsecondhalf:    
    FileReadByte $0 $2
done:    
    Pop $4
    Pop $3
    Pop $2
    Push $1
    Exch 
    Pop $1
    Exch 
    Pop $0
FunctionEnd  
this your code find '"Password"= line number and show number line in $R3.
now just i have one question and one help:

1-Question). in your code if i replace '"Password"=' by 'Password' then program don't show me line number, because correctly find word,whay? this code just search first line? for example i change that to this:
	StrCpy $R2 $R1 8 1
	StrCmp $R2 'Password' done
is way i no set length "from" till "finish"? (8-1) and just find word?

2-help).my program build is in finished level and this fix(remove line) is write final code it that...
now i use from ansi plugin and i cant change app and rebuild with unicode,i need a code for remove a line by line number from unicode text file.i searched but not found...
thanks mr Anders,mr stass,mr jpderuiter
r2du-soft#
Masters i fix problems but i have another problem...

in some of the text file i have Persian word and after remove password line,in new text created Persian words broken!



Before remove password word.(Original file):

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\NSIS]
@="C:\\Program Files\\NSIS"
"VersionMajor"=dword:00000003
"VersionMinor"=dword:00000000
"Password"=hex:0e,0e,f,00,43,05,43,00.43,43,43,43,43,00,9c,b5
"EngineOneError"="خطا در هنگام بار گزاری موتور"
"EngineTwoError"="خطا در هنگام بار گزاری موتور"
"FileGuardError"="هیچ موتوری بارگزاری نشد"
"MMError"="این سیستم در حالت اضطراری غیر فعال می باشد"
"LCError"="این سیستم در حالت اضطراری غیر فعال می باشد"
"VersionRevision"=dword:00000004
"VersionBuild"=dword:00000000


After remove password word.(Now i have this):

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\NSIS]
@="C:\\Program Files\\NSIS"
"VersionMajor"=dword:00000003
"VersionMinor"=dword:00000000
"EngineOneError"=".7' /1 GF¯'E ('1 ¯2'1? EH*H1"
"EngineTwoError"=".7' /1 GF¯'E ('1 ¯2'1? EH*H1"
"FileGuardError"="G?? EH*H1? ('1¯2'1? F4/"
"MMError"="'?F 3?3*E /1 -'D* '671'1? :?1 A9'D E? ('4/"
"LCError"="'?F 3?3*E /1 -'D* '671'1? :?1 A9'D E? ('4/"
"VersionRevision"=dword:00000004
"VersionBuild"=dword:00000000


But i need this after remove.(just want fix Persian words):

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\NSIS]
@="C:\\Program Files\\NSIS"
"VersionMajor"=dword:00000003
"VersionMinor"=dword:00000000
"EngineOneError"="خطا در هنگام بار گزاری موتور"
"EngineTwoError"="خطا در هنگام بار گزاری موتور"
"FileGuardError"="هیچ موتوری بارگزاری نشد"
"MMError"="این سیستم در حالت اضطراری غیر فعال می باشد"
"LCError"="این سیستم در حالت اضطراری غیر فعال می باشد"
"VersionRevision"=dword:00000004
"VersionBuild"=dword:00000000

i need fix values: (EngineOneError),(EngineTwoError),(FileGuardError),(MMError),(LCError) and all persian values is my file.
i can remove thats line but i need solve with fix.
thanks

i attachment .nsi script and text files.


[Write Update:]
i test http://nsis.sourceforge.net/Delete_l...line_inclusive (Delete lines from one line to another line inclusive) and try to delete a line by line number from my file,but this way not work on unicode text and after run exe app for remove a line from my text file all values in my file change to sample this: 楗摮睯⁳敒楧瑳祲䔠楤潴⁲敖獲潩⸵........

So far not made a plugin for remove a line from Unicode files?