Sign in to follow this  
Followers 0
byoda

String problems

6 posts in this topic

All,

I want to open a file, search the file for a keyword, grab 10 lines after the keyword and then write it into another file. I am able to open, search and locate the keyword, but I cant figure out how to get the 10 lines after the keyword and then writting them into a file. Thanks.

$file=FileOpen("C:\file.txt")

$read=FileRead($file)

If StringRegExp($read, "keyword")Then

;Copy the next 10 lines

;Write into another file

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

This would how I would attack the problem. I would use StringSplit and use $var[2] and split that by @CR, then from that array loop through it combine $var[1] through $var[11] in to one variable and save it to another file.

something like...

$filepart = StringSplit($file_contents,"keyword")
$lines = StringSplit($filepart[2],@CR)
$compiled_array = ""
For $i = 0 To 10
  $compiled_array &= $lines[$i+1] $ @CR;add back the line break
Next

My code isn't perfect though

Edited by clonetrooper9494

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Here my 1st idea:

Local $i, $x, $line, $file, $keyword

$keyword = "Line15"
$file = FileOpen("c:\file.txt", 0)
$new_file = FileOpen("c:\file_new.txt", 2)
$i = 2
While 1
    $line = FileReadLine($file)
    If @error = -1 Then ExitLoop
    If StringInStr($line, $keyword) > 0 Then
        For $x = $i To $i + 10
            FileWriteLine($new_file, FileReadLine($file, $x))
        Next
        ExitLoop
    EndIf
    $i += 1
WEnd
FileClose($file)
FileClose($new_file)

BR,

UEZ

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites

Here my 1st idea:

Local $i, $x, $line, $file, $keyword

$keyword = "Line15"
$file = FileOpen("c:\file.txt", 0)
$new_file = FileOpen("c:\file_new.txt", 2)
$i = 2
While 1
    $line = FileReadLine($file)
    If @error = -1 Then ExitLoop
    If StringInStr($line, $keyword) > 0 Then
        For $x = $i To $i + 10
            FileWriteLine($new_file, FileReadLine($file, $x))
        Next
        ExitLoop
    EndIf
    $i += 1
WEnd
FileClose($file)
FileClose($new_file)

BR,

UEZ

Worked like a champ. Thank you very much.

Share this post


Link to post
Share on other sites

Here my 1st idea:

Local $i, $x, $line, $file, $keyword

$keyword = "Line15"
$file = FileOpen("c:\file.txt", 0)
$new_file = FileOpen("c:\file_new.txt", 2)
$i = 2
While 1
    $line = FileReadLine($file)
    If @error = -1 Then ExitLoop
    If StringInStr($line, $keyword) > 0 Then
        For $x = $i To $i + 10
            FileWriteLine($new_file, FileReadLine($file, $x))
        Next
        ExitLoop
    EndIf
    $i += 1
WEnd
FileClose($file)
FileClose($new_file)

BR,

UEZ

One last thing. If I run 2 of these in a row, the first ouput in the file is erased. I thought it might be the FileWriteLine, so I changed it to FileWrite, but it still erases the first output. Any suggestions? Thanks.

Share this post


Link to post
Share on other sites

One last thing. If I run 2 of these in a row, the first ouput in the file is erased. I thought it might be the FileWriteLine, so I changed it to FileWrite, but it still erases the first output. Any suggestions? Thanks.

You should open the file in mode 1 -> "Write mode (append to end of file)". Take a look to FileOpen help

$new_file = FileOpen("c:\file_new.txt", 1)

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