Sign in to follow this  
Followers 0
bluerein

return full line from part text?

14 posts in this topic

Im trying to get a line from just part of its text and struggling finding anything..

example

I have a index.html and I need to search it for the text "@import" which is ok ,but I need to return the full line its on...

Any ideas

regards m-tek

Share this post


Link to post
Share on other sites



Maybe this will help

Func _SearchString()
    $File = FileOpen("yourfile.html")
    $Dat = FileRead($File)
    $Lines = StringSplit($Dat, @CR)
    For $I = 1 To $Lines[0]
        If StringInStr($Lines[$I], "@IMPORT") Then
            Return $Lines[$I]
            ExitLoop
        EndIf
    Next
EndFunc

Click here for the best AutoIt help possible.Currently Working on: Autoit RAT

Share this post


Link to post
Share on other sites

wow thanks for the help !!

Both work but return the required line plus 2 up and 2 down ...so its returning 5 lines

ANy ideas??

Share this post


Link to post
Share on other sites

no it doesnt ...

my file looks like this

sometext 

@import

sometext

and thats what it returns

now if I edit the file like so

sometext 
1111
@import
1111
sometext

filling in the gaps ..it returns correctly!!

hmmmmm

Share this post


Link to post
Share on other sites

Try StringStripWS()

Or add another If statement... If $aRecords[$i] <> "" Then... whatever for my code.

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

no its not the gaps !!

I just used URLDownloadToFile to get the file then

#include <file.au3>
      Dim $aRecords
      
      _FileReadToArray("index.html",$aRecords)
      
      For $i = 1 to $aRecords[0]
          If StringInStr($aRecords[$i], "@import") Then
              MsgBox(0,"found it", $aRecords[$i]);print the line that it was found on and all the text
              ExitLoop
          EndIf
      Next

to get the line

if I download the file first then open it then save it , it works

but if I dont open it then save it returns wrong...

how strange

try this

see if you get the same as me

#include <IE.au3>
#include <INet.au3>
#include <file.au3>
global $site="http://www.invisionmodding.com/index.php"
global $site1="http://www.autoitscript.com/forum/index.php"
URLDownloadToFile ($site,"index.html")
URLDownloadToFile ($site1,"index1.html")

Dim $aRecords

 _FileReadToArray("index.html",$aRecords)

For $i = 1 to $aRecords[0]
    If StringInStr($aRecords[$i], "@import") Then
        MsgBox(0,"found it", $aRecords[$i]);print the line that it was found on and all the text
         _FileReadToArray("index1.html",$aRecords)

For $i = 1 to $aRecords[0]
    If StringInStr($aRecords[$i], "@import") Then
        MsgBox(0,"found it", $aRecords[$i]);print the line that it was found on and all the text
        ExitLoop
    EndIf
Next
        ExitLoop
    EndIf
Next
Edited by bluerein

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

I got the same... the one index has a few LF's so they show up on new lines but the array doesn't split them up. You can do this to get rid of that. This should put it all on one line.

#include <file.au3>

Dim $aRecords

_FileReadToArray("index.html",$aRecords)

For $i = 1 to $aRecords[0]
    If StringInStr($aRecords[$i], "@import") Then
        MsgBox(0,"found it", StringReplace($aRecords[$i],@LF,""));print the line that it was found on and all the text
        ExitLoop
    EndIf
Next
Edited by SoulA

Share this post


Link to post
Share on other sites

could also do this.... not sure what you are looking for.

#include <file.au3>

Dim $aRecords

_FileReadToArray("index.html",$aRecords)

For $i = 1 to $aRecords[0]
    If StringInStr($aRecords[$i], "@import") Then
        $sString = StringTrimLeft($aRecords[$i], StringInStr($aRecords[$i], @LF, 0, 2))
        $sString = StringTrimRight($sString, StringLen($sString) - StringInStr($sString, @LF))
        MsgBox(0,"found it", $sString);print the line that it was found on and all the text
        ExitLoop
    EndIf
Next

Share this post


Link to post
Share on other sites

Hey blue,

I'm not quite sure what you want... The position? The full line text?

Use this pattern for SRE:

.*@import.*

Flag: 3

Want the position of any of the full lines?

StringInStr($String, $aSRE[0])oÝ÷ Øú,بÚ*+¶Æ®¶­se7G&ætå7G"b33cµ7G&ærÂgV÷C´×÷'BgV÷C²ÂÂ"

^^^ Finds the second occurrance

That help?

Share this post


Link to post
Share on other sites

#14 ·  Posted (edited)

Just to clear up Im just trying to get the whole line that has "@import" on it

but for some reason

#include <file.au3>
 
 Dim $aRecords
 
 _FileReadToArray("index.html",$aRecords)
 
 For $i = 1 to $aRecords[0]
     If StringInStr($aRecords[$i], "@import") Then
         $sString = StringTrimLeft($aRecords[$i], StringInStr($aRecords[$i], @LF, 0, 2))
         $sString = StringTrimRight($sString, StringLen($sString) - StringInStr($sString, @LF))
         MsgBox(0,"found it", $sString);print the line that it was found on and all the text
         ExitLoop
     EndIf
 Next

that returns one blank now and one correct.....

EDIT!!!

managed to get it to work ....Its a bit scrappy but none the less works so thanks everyone for your help

Edited by bluerein

Share this post


Link to post
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
Sign in to follow this  
Followers 0