Page 1 of 1

Found expression in a file and count lines

PostPosted: Tue Jul 08, 2014 6:39 am
by UltraVox
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 !

Re: Found expression in a file and count lines

PostPosted: Tue Jul 08, 2014 7:04 am
by dec
Hello UltraVox,

Take a look at npLineNumber and npLineNumberEx actions from my npUtil plugin.

Re: Found expression in a file and count lines

PostPosted: Tue Jul 08, 2014 7:45 am
by dpayer
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.

Re: Found expression in a file and count lines

PostPosted: Tue Jul 08, 2014 9:30 am
by UltraVox
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.

Re: Found expression in a file and count lines

PostPosted: Tue Jul 08, 2014 1:15 pm
by dec
Hello,

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


No problem. :)