• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 176
  • Last Modified:

Search a line then delete it plus other lines underneath it

I have a text file which lookes like this (only 100 times more lines...)

zone "meihuizentechniek.nl"{
        type master;
        file "/var/named/meihuizentechniek.nl.hosts";
};
zone "ariane-txt.nl"{
        type master;
        file "/var/named/ariane-txt.nl.hosts";
};
zone "roze-mini.nl"{
        type master;
        file "/var/named/roze-mini.nl.hosts";
};
zone "officecursussen.nl"{
        type master;
        file "/var/named/officecursussen.nl.hosts";
};
zone "dannenburg.nl"{
        type master;
        file "/var/named/dannenburg.nl.hosts";
};

I have to delete one block of text fom this file:

eg:
zone "officecursussen.nl"{
        type master;
        file "/var/named/officecursussen.nl.hosts";
};

What i want to do is search for the domainname: officecursussen.nl and then delete that line and the three line under it.
I have to do this serveral times.
0
Sanderk
Asked:
Sanderk
4 Solutions
 
GrahamSkanCommented:
You could create a new file from the original:

Private Sub Command1_Click()
    Dim f As Integer
    Dim g As Integer
    Dim strLine As String
   
    f = FreeFile
    Open "Myfile.txt" For Input As #f
        g = FreeFile
        Open "Newfile.txt" For Input As #f
            Do Until EOF(f)
                Line Input #f, strLine
                If InStr(strLine, "officecursussen.nl") > 0 Then
                    c = 4
                End If
                If c = 0 Then
                    Print #g, strLine
                Else
                    c = c - 1
                End If
            Loop
        Close #g
    Close #f
End Sub
0
 
senthil_msvCommented:
Private Sub Command1_Click()
Call RemoveUnwantedLinesInFiles("officecursussen.nl")
End Sub

Private Sub RemoveUnwantedLinesInFiles(ByVal svlUnWantedFileName As String)
On Error GoTo ErrorHandler
    Dim FreeFileNum As Integer
    Dim svlOrgFileName  As String
    Dim ReadFile, I, ArrReadFile
   
    FreeFileNum = FreeFile
   
    svlOrgFileName = App.Path & "\OrgFile.txt"
   
    Open svlOrgFileName For Input As #FreeFileNum
        ReadFile = Input(LOF(FreeFileNum), FreeFileNum) 'Load all the File Contents
    Close #FreeFileNum

    If ReadFile <> "" Then
        If Len(Dir(svlOrgFileName)) <> 0 Then Kill svlOrgFileName   'Delete the Existing File
        FreeFileNum = FreeFile
        ArrReadFile = Split(ReadFile, vbCrLf)
        Open svlOrgFileName For Output As #FreeFileNum  'Create the File with same Name
        If IsArray(ArrReadFile) Then
            For I = 0 To UBound(ArrReadFile)
                If InStr(1, UCase(Trim(ArrReadFile(I))), UCase(Trim(svlUnWantedFileName))) > 0 Then
                    I = I + 3
                Else
                    Print #FreeFileNum, ArrReadFile(I)  'Writes the contents into File
                End If
            Next I
        End If
        Close #FreeFileNum
    End If
Exit Sub
ErrorHandler:
    MsgBox Err.Number & " - " & Err.Description, vbCritical
End Sub
0
 
EDDYKTCommented:
senthil_msv's method will work, except it is more easy to split with keyword zone


ie

ArrReadFile = Split(ReadFile, "zone")
Open svlOrgFileName For Output As #FreeFileNum  'Create the File with same Name
       If IsArray(ArrReadFile) Then
           For I = 0 To UBound(ArrReadFile)
               If InStr(1, UCase(Trim(ArrReadFile(I))), UCase(Trim(svlUnWantedFileName))) > 0 Then
                   ArrReadFile(i) = ""
               End If
           Next I
       End If
        Print #FreeFileNum, join(ArrReadFile(I), "zone")  'Writes the contents into File
       Close #FreeFileNum
0
 
cjardCommented:


i'd just use a decent text editor with regular expression support. www.ultraedit.com is an example:

find

%zone "officecursussen.nl"^{*^p*^p*^p};$

replace:

(nothing)


go

:)
ahh, the power of regular expressions
0
 
cjardCommented:
slight typo correction, should you choose to use ultraedit, but note you need to use "ultraedit style syntax" in the regexp, not "unix style syntax" (configurable option):

find:
%zone "officecursussen.nl"{^p*^p*^p*};$

replace:
(nothing)
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now