Source language: Translate to:

Found expression in a file and count lines

Questions about using NeoBook's scripting language

Moderator: Neosoft Support

Found expression in a file and count lines

Postby UltraVox » Tue Jul 08, 2014 6:39 am

Hello all,

I would find a word or phrase in a file.txt (this is already done) and then determine which line number where it is.

For example, the phrase "Always the sun" is in the file concerned, but at what line number ?

Thanks for your help !
UltraVox
 
Posts: 419
Joined: Sat Jul 28, 2007 11:14 am
Location: France

Re: Found expression in a file and count lines

Postby dec » Tue Jul 08, 2014 7:04 am

Hello UltraVox,

Take a look at npLineNumber and npLineNumberEx actions from my npUtil plugin.
.
Enhance your NeoBook applications!
.
58 plugins, 1131 actions and 233 samples
.
NeoPlugins website: www.neoplugins.com
.
User avatar
dec
 
Posts: 1663
Joined: Wed Nov 16, 2005 12:48 am
Location: Spain

Re: Found expression in a file and count lines

Postby dpayer » Tue Jul 08, 2014 7:45 am

UltraVox wrote:Hello all,

I would find a word or phrase in a file.txt (this is already done) and then determine which line number where it is.

For example, the phrase "Always the sun" is in the file concerned, but at what line number ?

Thanks for your help !


Instead of searching each and every line in your string for the expression I suggest another path. It will also solve the issue if you have two instances of that phrase in a string.

Parse the string by the expression, then count the number of line separators in each parsed section.

Take this code and copy it into a blank pub (640x480) - look at the button code
Code: Select all
{NeoBook 5 Objects}
NeoBookVer=5.80
ObjectType=3
Name=PushButton1
X=22
Y=19
W=203
H=60
Anchor=0
Text=File to VAR
Align=2
ImageStyle=0
XPTheme=Yes
ObjAction=If "[searchedfortext]" "=" ""¶Alertbox "error" "need searched for text"¶GotoLine "endofscript"¶Endif¶FileOpenBox "Select File" "Any File|*.*" "c:\" "[selectedfile]" ""¶FileRead "[selectedfile]" "All" "[filecontent]"¶StrParse "[filecontent]" "[searchedfortext]" "[foundtext]" "[foundnum]"¶Loop "1" "[foundnum]" "[WILL]"¶.find how many 'lines' in each entry of variable array to determine position¶StrParse "[foundtext[will]]" "[#13][#10]" "[linecontent]" "[linenumberfound]"¶AlertBox "Line Number" "Searched for text:|[searchedfortext]|found on line:[linenumberfound]"¶Endloop¶:endofscript
LineColor=0
LineWidth=1
LineStyle=0
FillColor=12632256
FillPattern=0
Font=Arial
FontSize=10
FontStyle=0
FontCharset=1
TextColor=0
TabOrder=2
ObjectType=9
Name=searchedfortext
X=274
Y=19
W=318
H=59
Anchor=0
VarName=[searchedfortext]
Align=1
EditLen=0
LineColor=0
LineWidth=1
LineStyle=0
FillColor=16777215
FillPattern=0
Font=Arial
FontSize=10
FontStyle=0
FontCharset=1
TextColor=0
TabOrder=3
ObjectType=8
Name=Text1
X=283
Y=92
W=295
H=26
Anchor=0
Font=Arial
FontSize=10
FontStyle=0
FontCharset=1
TextColor=0
Text={\rtf1\ansi\deff0\deftab254{\fonttbl{\f0\fnil\fcharset1 Arial;}}{\pard{\ql\li0\fi0\ri0\sb0\sl\sa0 \plain\f0\fs20\cf0\i Searched for text}}}
HMargin=0
VMargin=0
LineColor=0
LineWidth=0
LineStyle=0
FillColor=16777215
FillPattern=0
TabOrder=4


button code:
Code: Select all
If "[searchedfortext]" "=" ""
Alertbox "error" "need searched for text"
GotoLine "endofscript"
Endif
FileOpenBox "Select File" "Any File|*.*" "c:\" "[selectedfile]" ""
FileRead "[selectedfile]" "All" "[filecontent]"
StrParse "[filecontent]" "[searchedfortext]" "[foundtext]" "[foundnum]"
Loop "1" "[foundnum]" "[WILL]"
.find how many 'lines' in each entry of variable array to determine position
StrParse "[foundtext[will]]" "[#13][#10]" "[linecontent]" "[linenumberfound]"
AlertBox "Line Number" "Searched for text:|[searchedfortext]|found on line:[linenumberfound]"
Endloop
:endofscript


I have the alertbox indicate the line number. You can adjust as you need on how to use that info.
Last edited by dpayer on Tue Jul 08, 2014 11:29 am, edited 1 time in total.
David Payer
Des Moines, Iowa
USA
User avatar
dpayer
 
Posts: 1384
Joined: Mon Apr 11, 2005 5:55 am
Location: Iowa - USA

Re: Found expression in a file and count lines

Postby UltraVox » Tue Jul 08, 2014 9:30 am

Thank you Dec and dpayer !

Dec, thank you for the plugin, I had forgotten this command :)

Dpayer, thank you for this script, simple and effective !

Thank you both.
UltraVox
 
Posts: 419
Joined: Sat Jul 28, 2007 11:14 am
Location: France

Re: Found expression in a file and count lines

Postby dec » Tue Jul 08, 2014 1:15 pm

Hello,

UltraVox wrote:Dec, thank you for the plugin, I had forgotten this command :)


No problem. :)
.
Enhance your NeoBook applications!
.
58 plugins, 1131 actions and 233 samples
.
NeoPlugins website: www.neoplugins.com
.
User avatar
dec
 
Posts: 1663
Joined: Wed Nov 16, 2005 12:48 am
Location: Spain


Return to NeoBook Action Commands

Who is online

Users browsing this forum: No registered users and 0 guests