- NSIS Discussion
- Read a word from a line
Archive: Read a word from a line
Alstom
22nd November 2006 09:08 UTC
Read a word from a line
hi, is it possible to read a word from a line?
Example: This is a cat.
If i want to read only the 2nd word, how do i do it??
I had used lineread, but it only read a whole line.
Is there any way to read just 1 word from a line??
bholliger
22nd November 2006 11:46 UTC
Hi Alstom!
Have a look at those functions
http://nsis.sourceforge.net/Docs/AppendixE.html
especially WordFind
Have a nice day!
Cheers
Bruno
Alstom
23rd November 2006 01:48 UTC
Thx Bruno
But is it possible to read a word on a line that would constantly be changed by the user?
Like on which line and which word to be read.
This is actually what i wanted to get.
bholliger
23rd November 2006 03:33 UTC
Yes, of course.
Just read the line with
${LineRead} "[File]" "$LineNumber" $var
and use
${WordFind} $var "[delimiter]" "$WordNumber" $R0
You can change $LineNumber and $WordNumber at runtime.
Otherwise post some real-world examples.
Alstom
23rd November 2006 03:43 UTC
ok, so how do i use it? What is [delimiter]?? $WordNumber is the number of the word in the sentence??
I would be changing the word in a few sentences in a file and the installer would be able to call the file to read the word i would be changing.
Could you show me a example to use this two together??
Example:
This is his dog.
How to alway read only the second word {is} or any word replacing the {is} in the sentence which could be changed anytime.
Do you understand my Q? Sorry, my english is not so gd. ;)
bholliger
23rd November 2006 04:16 UTC
This might help you:
;--------------------------------
;Include Modern UI
!include "MUI.nsh"
;--------------------------------
;General
Outfile test.exe
;--------------------------------
;Pages
!insertmacro MUI_PAGE_INSTFILES
;--------------------------------
;including files
!include TextFunc.nsh
!insertmacro LineFind
!include WordFunc.nsh
!insertmacro WordReplace
!insertmacro WordFind
Function "MyFunction"
; $R9 current line
; $R8 current line number
; $R7 current line negative number
; $R6 current range of lines
; $R5 handle of a file opened to read
; $R4 handle of a file opened to write ($R4="" if "/NUL")
; you can use any string functions
; $R0-$R3 are not used (save data in them).
; ...
${WordFind} "$R9" " " "+2" $R0 ; get the second word
${WordReplace} $R9 " $R0 " " was " "+" $R9 ; replace the word (including delimiters)
Push $0 ; If $var="StopLineFind" Then exit from function
; If $var="SkipWrite" Then skip current line (ignored if "/NUL")
FunctionEnd
Section "secDummy"
${LineFind} "test.txt" "" "2" "MyFunction" ; 2 = line number
SectionEnd
;--------------------------------
;Languages
!insertmacro MUI_LANGUAGE "English"
Content of file test.txt before:
This is my cat.
This is my dog.
This is my cow.
Content of file test.txt after:
This is my cat.
This was my dog.
This is my cow.
Alstom
24th November 2006 08:07 UTC
Hi , that was great!! Now i would wish to do a loop to read a file and get some content in the file til end of file.
How do i write the loop??
Example:
Read the file
While(Not end of file)
Do line read
Do Word find
IF $R1 ;value of the word
Do ..........
Else
Do ..........
How do i write the actual code for this??
bholliger
24th November 2006 08:12 UTC
Hi Alstom!
To achieve this, just read the documentation for LineFind at [1] and use the skeleton provided above.
${LineFind} "test.txt" "" "1:-1" "MyFunction"
Cheers
Bruno
[1]
http://nsis.sourceforge.net/Docs/AppendixE.html#E.2.2
Alstom
24th November 2006 08:15 UTC
Hi, I am able to do the lineread and wordfind to get the word i want but how to do a loop for a file with many lines to check the word and call a function if it is the word i want.
If $R1 = A
Do .....
Alstom
24th November 2006 08:42 UTC
hi, maybe i put a simple example of what i want to do below.
Example:
WHILE ($R0 != END OF FILE)
${WordFind} "$R1 " "," "-2" $R1 ; Read the last 2nd word from current line
IF $R1 = A
CopyFiles "$DESKTOP\file.exe" "$OUTDIR"
ExecWait "$OUTDIR\file.exe"
ELSE
MessageBox MB_Ok "$$R1 not correct"
$R1++ ;go to next line
bholliger
24th November 2006 08:56 UTC
The third parameter of LineFind is to specify the range of lines you want to read! You don't need to use something like while!
bholliger
24th November 2006 09:03 UTC
I think you want to parse a licence file. You could parse your license file in .onInit with LineFind and activate the sections with SelectSection from Sections.nsh. If you want to use the components dialog as well just add hidden sections to your script, which you enable from the license file, and show those the user is allowed to choose.
Alstom
27th November 2006 06:28 UTC
How to write not end of file in a while loop??
${WHILE} R0 != end of file??
What the code for end of file??
bholliger
27th November 2006 06:39 UTC
Here you are:
${LineFind} "test.txt" "" "1:-1" "MyFunction"
1:-1 symbolises the whole file
Alstom
27th November 2006 06:44 UTC
i just wan the end of the file.
for while loop.
While $R1 < "end of file"
bholliger
27th November 2006 07:10 UTC
http://nsis.sourceforge.net/Docs/AppendixE.html#E.2.2
use a negative value (-1) for the last line.
Alstom
28th November 2006 01:21 UTC
sorry but not able to work when i i wan to put it in a while loop. how to let the loop go to the end of file??
i have put a $R0 $R0 + 1 before the end of the loop to go to the next line but how to go to the last line of the line to stop??
for the moment , i put :
WHILE $R0 <100
but how to replace the 100 with the end of file???
bholliger
28th November 2006 01:30 UTC
Actually, I don't understand why you want to use a while loop! Just use LineFind, that's great.
Alstom
28th November 2006 01:34 UTC
i using a line read and wordfind to go from a line to the other line 1 by 1 to call some function if the word is found on each line.
bholliger
28th November 2006 03:38 UTC
Ok, have a look at the post from 23. November.
LineFind is even better because you don't have to worry about the size of the file. LineFind calls your function for every line you specify and you get the index in a variable in the function:
; $R9 current line <--
; $R8 current line number <---
; $R7 current line negative number
; $R6 current range of lines
; $R5 handle of a file opened to read
; $R4 handle of a file opened to write ($R4="" if "/NUL")
This calls MyFunction for every line in the file. If you need only the last, just adjust the range as you see in the docs.
${LineFind} "test.txt" "" "1:-1" "MyFunction"
sunlight112
30th November 2006 03:51 UTC
Hi bholliger,
I has problems which related with WordFind and I post my question here.
I also try with your example which you posted above.
I try with it and it do well except one case.
Url = jdbc:mysql://127.0.0.1:3306/Openim
I has put this line in file text in c:\test.txt
Firstly, I try with this
This is my cat
And I can put "jdbc:mysql://127.0.0.1:3306/Openim" in "THIS IS MY CAT" to become "THIS jdbc:mysql://127.0.0.1:3306/Openim MY CAT"
However, I try with this
"Url = jdbc:mysql://127.0.0.1:3306/Openim" Nothing change after I run your code above!
${WORDFIND}is not work in my case, is it?
Can you help me!
My word is too bad, if you do not understand.Please tell me!
Thank in advance,
bholliger
30th November 2006 03:57 UTC
Sorry, I don't get what you want to achieve.
Could you explain it in another way? What do you want to change in your URL = ...?
sunlight112
30th November 2006 04:14 UTC
Hi bholliger,
I want to change URL to run my webadmin if they run webadmin in their computer or they run it on the other computer. So, I need IP address and change URL.
I try your code and I change to like this
Function "MyFunction"
; $R9 current line
; $R8 current line number
; $R7 current line negative number
; $R6 current range of lines
; $R5 handle of a file opened to read
; $R4 handle of a file opened to write ($R4="" if "/NUL")
; you can use any string functions
; $R0-$R3 are not used (save data in them).
; ...
${WordFind} "$R9" " " "+3" $R0 ; get the third word
MessageBOx MB_OK "$$R9 (string): $R9"
MessageBOx MB_OK "$$R0 (word to replace): $R0"
${WordReplace} $R9 " $R0 " " jdbc:mysql://193.168.75.124:3306/Openim " "+" $R9 ; replace the word (including delimiters)
MessageBOx MB_OK "$$R9 after replace: $R9"
Push $0 ; If $var="StopLineFind" Then exit from function
; If $var="SkipWrite" Then skip current line (ignored if "/NUL")
FunctionEnd
Section "secDummy"
${LineFind} "c:\test.txt" "" "4" "MyFunction" ; 2 = line number
SectionEnd
My test.txt
This is my cat.
This was my dog
This is my cow.
Url = jdbc:mysql://127.0.0.1:3306/Openim
This is one case which I must change in file webadmin. Also,I can not change it although I try with other case like
Username = root or Password = 123456
[
With WORDFIND and WORDREPLACE, I can not do if in my text has "="
I do not know why it is not work?
If you know, please tell me!
Thank in advance,
bholliger
30th November 2006 04:37 UTC
For your sample I'd use WordFind2X because there is a certain structure. :// and :
StrCpy $R2 "192.168.0.24" ; new IP
${WordFind2X} "$R9" "://" ":" "+1" $R0 ; get current value
MessageBOx MB_OK "$$R9 (string): $R9"
MessageBOx MB_OK "$$R0 (word to replace): $R0"
MessageBOx MB_OK "$$R2 (IP): $R2"
${WordReplace} $R9 "$R0" "$R2" "+" $R9
MessageBOx MB_OK "$$R9 after replace: $R9"
If you have a file with this structure:
Username = root
Password = 123456
use ConfigRead and ConfigWrite from [1]. It's a lot easier and you don't have to filter or know where your value in the file is.
[1]
http://nsis.sourceforge.net/Docs/AppendixE.html#E.2.9
Have fun!
Cheers
Bruno
sunlight112
30th November 2006 06:13 UTC
Thank bholliger very much for your help
Now I can solve my problems
:)
bholliger
30th November 2006 06:18 UTC
You're welcome! :-)
sunlight112
30th November 2006 09:45 UTC
Hi bholliger :)
I still have one question which is related with change word in file.
My file which I intend to change it influence my application.
If I change some words by hand in that file, I can run my web well. But I try with $WORDFIND, $CONFIGWRITE and $WORDREPLACE, at that time, my application is error when it runs.
I do not know why although I check file which I change.It's ok.
I wonder I use $WORDFIND... in NSIS does it display some characters in my file and it also is problems which my application is not run when I repair it.
Did you use to see this problem? I also search these errors on google but they just said "it is problem in software".In my mind, I do not think so.
Do you have any opinions about my case?
Thank a lot for your help,
:)
bholliger
30th November 2006 10:34 UTC
Could you post your code, please?
How does the file look before/after?
sunlight112
1st December 2006 01:33 UTC
Thank bholliger,
My code here:
Function Replace
StrCpy $R2 $IP ;new IP
Detailprint "$$IP : $IP"
${WordFind2X} "$R9" "://" ":" "+1" $R0 ; get current value
Detailprint "$$R9 truoc khi thay the: $R9"
Detailprint "$$R0 tu de thay the: $R0"
Detailprint "$$R2 (IP): $R2"
${WordReplace} $R9 "$R0" "$R2" "+" $R9
; ${WordReplace} $R9 " $R0 " " jdbc:mysql://127.0.0.1:3306/Openim " "+" $R9 ; replace the word (including delimiters)
Detailprint "$$R9 sau khi thay the: $R9"
Push $0 ; If $var="StopLineFind" Then exit from function
; If $var="SkipWrite" Then skip current line (ignored if "/NUL")
FunctionEnd
InstallDirRegKey HKLM "SOFTWARE\Apache Software Foundation\Tomcat\5.5" "InstallPath"
Section EditFile
StrCpy $IP "localhost"
StrCpy $root "root"
StrCpy $passroot "123456"
StrCpy $domain "telnet.com"
StrCpy $useradmin "admin"
StrCpy $passadmin "password"
nsExec::Exec 'net stop "Apache Tomcat 5"'
Detailprint "Stop Tomcat successfull"
StrCpy $Path "$INSTDIR\webapps\webadmin\WEB-INF\classes\ApplicationResources.properties"
Detailprint "$$Path :$Path"
#### Change hostname
${LineFind} "$Path" "" "2" "Replace" ; 2 = line number
#### Change root and password of database
${ConfigWrite} "$Path" "Username" " = $root" $1
Detailprint "$$1: $1"
${ConfigWrite} "$Path" "Password" " = $passroot" $1
Detailprint "$$1: $1"
#### Change Domain
${ConfigWrite} "$Path" "domainname" "=$domain" $1
Detailprint "$$1: $1"
#### Change rootuser and passworduser
; StrCpy $domain "adminroot"
${ConfigWrite} "$Path" "Rootuser" "=$useradmin" $1
Detailprint "$$1: $1"
${ConfigWrite} "$Path" "Rootpassword" "=$passadmin" $1
Detailprint "$$1: $1"
nsExec::Exec 'net start "Apache Tomcat 5"'
SectionEnd
File before I change
[QUOTE]
Driver = com.mysql.jdbc.Driver
Url = jdbc:mysql://127.0.0.1:3306/Openim
Username = root
Password = 123456
domainname=wwtkk.com
RootUser=rootadm
RootPassword=password
{/QUOTE]
My file after I execute my installer
Driver = com.mysql.jdbc.Driver
Url = jdbc:mysql://localhost:3306/Openim
Username = root
Password = 123456
domainname=telnet.com
RootUser=admin
RootPassword=password
I only change these lines in my file. I can not explain why if I install with my installer,my web application is error. When I change by hand, it works well.
If you know, please tell me why?
Thank in advance,
bholliger
1st December 2006 03:44 UTC
What's the error message of the application? Is your computer resolving the localhost correctly to 127.0.0.1?
Although this is not the root of the problem, I'd use ConfigWrite like that:
${ConfigWrite} "$Path" "Rootuser=" "$useradmin" $1
and I'd use ${ConfigRead} to read the value of Url=, otherwise you have to make sure that this setting always appears on the second line.
sunlight112
1st December 2006 04:39 UTC
Hi bholliger
HTTPステータス 500 -
--------------------------------------------------------------------------------
type 例外レãƒãƒ¼ãƒˆ
メッセージ
説明 The server encountered an internal error () that prevented it from fulfilling this request.
例外
javax.servlet.ServletException
org.apache.struts.action.RequestProcessor.processException(RequestProcessor.java:545)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:486)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:525)
javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
原å›_
java.lang.NullPointerException
DataObject.Factory.UserFactory.CheckUserNamePassword(UserFactory.java:133)
webadmin.loginAction.execute(loginAction.java:43)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:484)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:525)
javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
æ³¨æ„ åŽŸå›_ã®ã™ã¹ã¦ã®ã‚¹ã‚¿ãƒƒã‚¯ãƒˆãƒ¬ãƒ¼ã‚¹ã¯ã€Apache Tomcat/5.5.20ã®ãƒ_ã‚°ã«è¨˜éŒ²ã•ã‚Œã¦ã„ã¾ã™
This is error when I run my web application.
I am sure when I use localhost on my computer, I can run my application.
And I also check carefully in file which I change by my installer.
It is the same with file which I change by hand.
But it can not run if I run installer to repair some characters in file by $WORDFIND....
Now I can not find a solution to do it.
Did you use to see this problem?
Please help me!
bholliger
1st December 2006 04:46 UTC
Hmmm. Obviously there are some problems with the login. Which password is used? The Username= or RootUser=?
sunlight112
1st December 2006 06:21 UTC
Password is used to logging is RootUser and Rootpassword
bholliger
1st December 2006 07:37 UTC
Compare a working copy and an edited copy of the file with Beyond Compare http://www.scootersoftware.com/download.php
The Hex-Module might show the difference, if there's one...
http://www.scootersoftware.com/download.php?c=hexview
sunlight112
1st December 2006 10:27 UTC
Thank bholliger,
I will try with this.