I am currently开发者_如何转开发 looking for a way of deleting every (n)th and (n)th line in a txt file. For example every 2nd and 5th line. Is there a way of doing this with a script or with C#?
This code is in VB.NET but I believe this will do what you want?
Dim sr As streamreader = Nothing
Dim sw As StreamWriter = Nothing
Dim LineString As String = ""
Dim LineNum As Integer = 0
Try
sr = New StreamReader("C:\scratch\input.txt")
sw = New StreamWriter("c:\scratch\output.txt")
Do Until sr.EndOfStream
LineString = sr.ReadLine
LineNum += 1
If LineNum Mod 2 = 0 Then
'don't output 2nd line
ElseIf LineNum Mod 5 = 0 Then
'don't output 5th line
Else
'write it
sw.WriteLine(LineString)
End If
Loop
Catch ex As Exception
MsgBox("Error - " & ex.Message)
Finally
If Not IsNothing(sr) Then sr.Close()
If Not IsNothing(sw) Then sw.Close()
End Try
精彩评论