Sign in to follow this  
Followers 0
dkwokgs

How read lines that I want to an array and replace

6 posts in this topic

Hi there,

Two question.

1. How can I read only lines that I want into an array?

eg. only wants to read starting with 10.151.10.10

IN FILES:-

10.151.10.10 www.corp.com

10.151.200.1 www.corp10.com

10.151.10.10 www.abcd.com

10.151.100.1 www.corp5.com

10.151.105.1 www.corp10.com

10.151.10.10 www.a123.com

2. How do I delete/replace lines (random) with another entries

Best Regards

Daniel

Share this post


Link to post
Share on other sites



Dim $FoundLines
$OpenedTextFile = FileOpen("yourfile.txt", 0)
If $OpenedTextFile = -1 Then
   MsgBox(64, "Error!", "File could not be opened!")
   Exit
EndIf

While 1
   $Line = FileReadLine($OpenedTextFile)
   If @error Then
      ExitLoop
   Else
      If StringInStr($Line, "10.151.10.10") Then
         $FoundLines = $FoundLines & $Line & @LF
      EndIf
   EndIf
WEnd

$FoundLines = StringTrimRight($FoundLines, 1)
$FoundLinesArray = StringSplit($FoundLines, @LF)

Share this post


Link to post
Share on other sites

Thanks. But how to put back the alter string to the file replacing the previous

Share this post


Link to post
Share on other sites

FileCopy("yourfile.txt", @TempDir & "\yourfile.txt")
$OpenedTextFile = FileOpen("yourfile.txt", 2)
$OpenedTempFile = FileOpen(@TempDir & "\yourfile.txt", 0)
If $OpenedTempFile = -1 Then
   MsgBox(64, "Error!", "File could not be opened!")
   Exit
EndIf

While 1
   $Line = FileReadLine($OpenedTempFile)
   If @error Then
      ExitLoop
   Else
      If StringInStr($Line, "10.151.10.10") Then
         FileWriteLine($OpenedTextFile, $Line)
      EndIf
   EndIf
WEnd

FileClose($OpenedTextFile)
FileClose($OpenedTempFile)

Share this post


Link to post
Share on other sites

Sorry, I think u got me wrong.

I want to replace the strings that I want with another strings but retain the rest.

IN FILES:-

10.151.10.10 www.corp.com replace with 10.151.10.124 www.corp.com

10.151.200.1 www.corp10.com

10.151.10.10 www.abcd.com replace with 10.151.10.124 www.abcd.com

10.151.100.1 www.corp5.com

10.151.105.1 www.corp10.com

10.151.10.10 www.a123.com replace with 10.151.10.124 www.a123.com

Share this post


Link to post
Share on other sites

Can't you be grateful for my time and try to finish the script yourself :)

FileCopy("yourfile.txt", @TempDir & "\yourfile.txt")
$OpenedTextFile = FileOpen("yourfile.txt", 2)
$OpenedTempFile = FileOpen(@TempDir & "\yourfile.txt", 0)
If $OpenedTempFile = -1 Then
   MsgBox(64, "Error!", "File could not be opened!")
   Exit
EndIf

While 1
   $Line = FileReadLine($OpenedTempFile)
   If @error Then
      ExitLoop
   Else
      If StringInStr($Line, "10.151.10.10") Then
         $NewLine = StringReplace($Line, "10.151.10.10", "10.10.10.10")
         FileWriteLine($OpenedTextFile, $NewLine)
      EndIf
   EndIf
WEnd

FileClose($OpenedTextFile)
FileClose($OpenedTempFile)

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