 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Author |
Message |
enrica
Joined: 21 Mar 2005 Posts: 117 Location: Bahia, Brasil
|
Posted: Tue Apr 11, 2006 7:45 pm Post subject: search a word (and 10 lines before and after) in txt file |
|
|
Hi, everybody,
With this one i find a line content a "userinput" word, is possible see also the 10 lines before and after the searched word?
| Code: | InputBox, UserInput,. search , , , 200, 85, 300, 380, , ,
FileDelete, C:\result.txt
Sleep, 500
Loop, read, C:\test.txt, C:\result.txt
{
IfInString, A_LoopReadLine, %UserInput%, FileAppend, %A_LoopReadLine%`n
}
Sleep, 500
FileRead, Clipboard, C:\result.txt
Sleep, 500
Run, C:\result.txt
Return |
Thanks in advance.
Regards, enrica. _________________ 65.6E.72.69.63.61. (My hovercraft is full of eels) |
|
| Back to top |
|
 |
garath
Joined: 24 Mar 2005 Posts: 372 Location: germany
|
Posted: Tue Apr 11, 2006 7:56 pm Post subject: |
|
|
first use fileread to get all the text into a variable
Use stringGetPos to get the location of the search text, then use stringgetpos with the offset you got, to find the 10 following returns ('n), then use Stringmid to get the 10 portions of text. |
|
| Back to top |
|
 |
enrica
Joined: 21 Mar 2005 Posts: 117 Location: Bahia, Brasil
|
Posted: Tue Apr 11, 2006 8:18 pm Post subject: |
|
|
Thank you, garath.
Now I'm stuck here:
| Code: | FileRead, var, C:\H\xyz.txt
InputBox, UserInput,. search , , , 200, 85, 300, 380, , , sleep
StringGetPos, pos, var, %UserInput%
MsgBox, The string was found at position %pos%.
StringGetPos, pos2, var, %pos%
MsgBox, 2 = %pos2%. |
Can you help me more, please?
Thank you!
ciao! _________________ 65.6E.72.69.63.61. (My hovercraft is full of eels) |
|
| Back to top |
|
 |
Pasukun
Joined: 16 Dec 2004 Posts: 84
|
Posted: Tue Apr 11, 2006 10:38 pm Post subject: |
|
|
This is probably not the best way, but hopely it can be some help to your problem.
Also, I didn't test it so.. it might not work correctly.
| Code: | InputBox, UserInput,. search , , , 200, 85, 300, 380, , ,
FileDelete, C:\result.txt
Var_Count = 1
Loop, read, C:\test.txt
{
IfInString, A_LoopReadLine, %UserInput%
{
Break
}
Var_Count++
}
If Var_Count <= 10
{
Var_Position = %Var_Count%
}
Else
{
Var_Position := Var_Count - 10
}
Loop, 21
{
FileReadLine, Var_Output, C:\test.txt, %Var_Position%
Var_Position++
If Var_Output =
{
Continue
}
FileAppend, %Var_Output%`n, C:\Result.txt
}
Run, C:\Result.txt
Return
|
_________________
"the things we touch have no permanence. my master would say: there is nothing we can hold onto in this world.. only by letting go can we truly possess what is real..." |
|
| Back to top |
|
 |
garath
Joined: 24 Mar 2005 Posts: 372 Location: germany
|
Posted: Tue Apr 11, 2006 10:57 pm Post subject: |
|
|
Sorry, I am tiered, but try the following code, it will give you the 10 lines before and after
| Code: | FileRead, Text_example, adress.ini
InputBox, UserInput,. search , , , 200, 85, 300, 380, , , sleep
StringGetPos, Search_pos, Text_example, %UserInput%
MsgBox, The string was found at position %Search_pos%.
StringGetPos, Line_10_u, Text_example, `n, L10, %Search_pos% ;Position of ten Lines +
Search_pos_d := StrLen(Text_example) - Search_pos
StringGetPos, Line_10_d, Text_example, `n, R10, %Search_pos_d% ; ;Position of ten Lines -
Result_count := Line_10_u - Line_10_d
StringMid, Result_String, Text_example, Line_10_d, Result_count
MsgBox, 2 = %Result_String%. |
|
|
| Back to top |
|
 |
PhiLho
Joined: 27 Dec 2005 Posts: 6721 Location: France (near Paris)
|
Posted: Tue Apr 11, 2006 11:14 pm Post subject: |
|
|
| Code: | searchedWord = Styles
;~ searchedWord = lexical
;~ searchedWord = LoopFileTimeCreated
linesAround := 10
linesAround++ ; Easier to manage...
beforeIndex := 0
afterIndex := 0
Loop Read, AutoHotkeySyntaxHighlighting.txt
{
if (afterIndex = 0)
{
; Normal mode, we store the read lines in a rotating array
beforeIndex := Mod(beforeIndex, linesAround)
linesBefore%beforeIndex% := A_LoopReadLine
beforeIndex++
}
If (InStr(A_LoopReadLine, searchedWord))
{
; Yeah! We found it!
afterIndex := 1
foundLine := A_Index
Continue ; Line already stored
}
If (afterIndex > 0)
{
; Found: we store the lines after the find
linesAfter%afterIndex% := A_LoopReadLine
afterIndex++
If (afterIndex = linesAround)
Break ; We got the needed number of lines after the find
}
}
res =
If (foundLine <= linesAround)
{
index := 0
beforeCount := foundLine
}
Else
{
index := beforeIndex
beforeCount := linesAround
}
Loop %beforeCount%
{
line := linesBefore%index%
If (A_Index = beforeCount) ; Found line
res = %res%<%index%>%line%`n
Else
res = %res%(%index%)%line%`n
index := Mod(index + 1, linesAround)
}
Loop % afterIndex - 1
{
line := linesAfter%A_Index%
res = %res%[%A_Index%]%line%`n
}
MsgBox Found at line: %foundLine%`n%res%
| Tested, but it is late, I didn't made tests for cases when there are less than 10 lines before or after the found line.
[EDIT] Now it supports finds near start or end of file. Added comments, too... _________________
vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")
Last edited by PhiLho on Wed Apr 12, 2006 9:44 am; edited 1 time in total |
|
| Back to top |
|
 |
enrica
Joined: 21 Mar 2005 Posts: 117 Location: Bahia, Brasil
|
Posted: Wed Apr 12, 2006 12:18 am Post subject: |
|
|
Pasukun, Garath & PhiLho, Thank You very much!
I know, is late! (in my dear Old Europe)
Yours efforts are very much appreciate!
Every solution work very well, now, the only thing very difficult is the choice!!!!
Thank You very much again!!!
ciao! enrica  _________________ 65.6E.72.69.63.61. (My hovercraft is full of eels) |
|
| Back to top |
|
 |
PhiLho
Joined: 27 Dec 2005 Posts: 6721 Location: France (near Paris)
|
Posted: Wed Apr 12, 2006 6:16 am Post subject: |
|
|
I prefer garath's solution, probably the faster (no script loop, using fully AutoHotkey capabilities) and the most reliable (probably behaving well on limits, thanks to AHK's internal tests) and the simpliest...
I overlooked the L#/R# part in StringGetPos, so reading first post of garath, I thought about loops. But this option makes his solution simple and elegant, he wins!
Unless you have to manage 10GB log files, in this case, my version is less memory intensive, and can even be faster if the searched string is near the top of the file!
PS.: I compared to the results of my code, perhaps it needs a little adjustment of the values (L11 and R11). _________________
vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2") |
|
| Back to top |
|
 |
garath
Joined: 24 Mar 2005 Posts: 372 Location: germany
|
Posted: Wed Apr 12, 2006 8:35 am Post subject: |
|
|
@Philho
I use something like this, to process a text-log, which is updated each second. I need to get only the last 40 lines containing a special Tag of a 10MB Text-log. The script is quite slow , maybe I will try your solution.
I didn't made tests for cases when there are less than 10 lines before or after the found line, too.  |
|
| Back to top |
|
 |
|
|
You can post new topics in this forum You can reply to topics in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|