AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Searching .txt for a value & copy that line and NEXT lin

 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
stilly



Joined: 21 May 2008
Posts: 2

PostPosted: Wed May 21, 2008 12:03 pm    Post subject: Searching .txt for a value & copy that line and NEXT lin Reply with quote

I'm new to this game and would appreciate some assistance. I have many text files and would like to search each one for any line that contains the text " <space>000, " then append that line AND the following line to a new file.

This is what I have so far...

Code:

#SingleInstance Force

SetWorkingDir %A_ScriptDir%

FileSelectFile, SelectedFile, 3, C:\Current.Job , Open a file, Text Documents (*.txt; *.PRN)

FileDelete NewFile.txt
Loop, read, %SelectedFile%, NewFile.txt
{
    IfInString, A_LoopReadLine, %A_SPACE%000`,
    {
          NewString := RegExReplace( A_LoopReadLine, "`n)(.+?)`r`n(.+?)`n", "$1 $2`n" )
          FileAppend, %NewString%`n , NewFile.txt
    }
}
return
Run, open "NewFile.txt"


...but it doesn't work at the RegExReplace bit. If I leave this line out the code works but only appends the line that contans the search term.
Any assistance and advice would be really appreciated. Love the software but am overwhelmed by how complex it all seems.
Back to top
View user's profile Send private message
HugoV



Joined: 27 May 2007
Posts: 650

PostPosted: Wed May 21, 2008 1:14 pm    Post subject: Reply with quote

Below an example without RegExReplace. In your case it doesn't work
because your search in A_LoopReadLine which is by definition one line.
The example below simply stores the previousline if it contains the
string, reads the next line and append both to the text file.
Code:
file =
(join`r`n
abc
def
 000,123
this line too
ghi
klm
 000,456
and this line too
nop
)

Loop, Parse, file, `r,`n
   {
   IfInString, PreviousLine, %A_SPACE%000`,
      {
      FileAppend, %PreviousLine% %A_LoopField%`n, NewFile.txt
      PreviousLine= ; clear
      }
   IfInString, A_LoopField, %A_SPACE%000`,
      PreviousLine:=A_LoopField
   }
Back to top
View user's profile Send private message
Extreminador



Joined: 05 May 2008
Posts: 22

PostPosted: Wed May 21, 2008 1:29 pm    Post subject: Reply with quote

Use the command FileReadLine instead and read the lines one by one...
Back to top
View user's profile Send private message
stilly



Joined: 21 May 2008
Posts: 2

PostPosted: Thu May 22, 2008 7:22 pm    Post subject: Reply with quote

thanks for your help, I'll give that a go. There's a lot more I ultimately want to accomplish but one step at a time I suppose.
Smile
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group