 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Author |
Message |
stilly
Joined: 21 May 2008 Posts: 2
|
Posted: Wed May 21, 2008 12:03 pm Post subject: Searching .txt for a value & copy that line and NEXT lin |
|
|
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 |
|
 |
HugoV
Joined: 27 May 2007 Posts: 650
|
Posted: Wed May 21, 2008 1:14 pm Post subject: |
|
|
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 |
|
 |
Extreminador
Joined: 05 May 2008 Posts: 22
|
Posted: Wed May 21, 2008 1:29 pm Post subject: |
|
|
| Use the command FileReadLine instead and read the lines one by one... |
|
| Back to top |
|
 |
stilly
Joined: 21 May 2008 Posts: 2
|
Posted: Thu May 22, 2008 7:22 pm Post subject: |
|
|
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.
 |
|
| 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
|