刪除文本文件中相同的行!
如果不使用數(shù)據(jù)庫,怎么樣才能刪除文本文件中相同的行呢?手工??。?0w行的記錄你試試手工????
下面這個(gè)腳本演示了如果刪除 文本文件中 相同的行!
Const adOpenStatic = 3
Const adLockOptimistic = 3
Const adCmdText = &H0001
Set objConnection = CreateObject("ADODB.Connection")
Set objRecordSet = CreateObject("ADODB.Recordset")
Const ForReading = 1, ForWriting = 2, ForAppending = 8
strPathToTextFile = "D:\"
strFile = "done.txt"
objConnection.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & strPathtoTextFile & ";" & _
"Extended Properties=""text;HDR=NO;FMT=Delimited"""
objRecordSet.Open "Select DISTINCT * FROM " & strFile, _
objConnection, adOpenStatic, adLockOptimistic, adCmdText
Do Until objRecordSet.EOF
str = objRecordSet.Fields.Item(0).Value
Dim fso, f
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile("D:\namelist.txt", ForAppending, True)
f.WriteLine str
f.Close
objRecordSet.MoveNext
Loop
D:\done.txt 是原始文件
D:\namelist.txt 是生成后的文件
把上面這些代碼,復(fù)制到一個(gè)txt文件中,修改成你所要的功能
另存為 *.vbs 后綴名就可以了!
===================================================
如何以A、B列為條件刪除相同行
我想設(shè)定用宏直接刪除相同行(比如:A1、B1與A4、B4數(shù)據(jù)相同則刪除A4、B4行),請(qǐng)大師們幫助一下。謝謝!
2006-11-13 16:29 HOmT398
參考
[CODE]
Sub test()
Dim mColl As New Collection
Dim iRow As Long, i As Long
Dim pt As Range
On Error Resume Next
With Sheet1
iRow = .[a65536].End(xlUp).Row
For i = 3 To iRow
mColl.Add CStr(.Cells(i, 1) & .Cells(i, 2)), CStr(.Cells(i, 1) & .Cells(i, 2))
If Err.Number <> 0 Then
If pt Is Nothing Then
Set pt = .Cells(i, 1)
Else
Set pt = Union(pt, .Cells(i, 1))
End If
Err.Clear
End If
Next
End With
pt.EntireRow.Select
End Sub
[/CODE]
2006-11-13 16:30 HOmT398
替換空格一類的自己做 :P
如果數(shù)據(jù)很多的話加一句:
application.ScreenUpdating=False
[[i] 本帖最后由 HOmT398 于 2006-11-13 16:31 編輯 [/i]]
2006-11-13 17:37 yky888
版主的方法很好!
進(jìn)一步請(qǐng)教:我需要把d列的數(shù)據(jù)也加為條件,您的代碼可作怎樣的變通呢?
請(qǐng)看我的附件。謝謝!
2006-11-13 17:43 HOmT398
[quote]原帖由 [i]yky888[/i] 于 2006-11-13 17:37 發(fā)表
版主的方法很好!
進(jìn)一步請(qǐng)教:我需要把d列的數(shù)據(jù)也加為條件,您的代碼可作怎樣的變通呢?
請(qǐng)看我的附件。謝謝! [/quote]
參考
[code]
Sub test()
Dim mColl As New Collection
Dim iRow As Long, i As Long
Dim pt As Range
Dim strKey As String
On Error Resume Next
With Sheet1
iRow = .[a65536].End(xlUp).Row
For i = 3 To iRow
strKey = CStr(.Cells(i, 1) & .Cells(i, 2) & .Cells(i, 4))
mColl.Add strKey, strKey
If Err.Number <> 0 Then
If pt Is Nothing Then
Set pt = .Cells(i, 1)
Else
Set pt = Union(pt, .Cells(i, 1))
End If
Err.Clear
End If
Next
End With
pt.EntireRow.Select
End Sub
[/code]
2006-11-13 17:45 HOmT398
回復(fù) #4 yky888 的帖子
再改一改
[code]Sub test()
Dim mColl As New Collection
Dim iRow As Long, i As Long
Dim pt As Range
Dim strKey As String
On Error Resume Next
Application.ScreenUpdating = False
With Sheet1
iRow = .[a65536].End(xlUp).Row
For i = 3 To iRow
strKey = CStr(.Cells(i, 1) & .Cells(i, 2) & .Cells(i, 4))
If Len(strKey) > 0 Then
mColl.Add strKey, strKey
If Err.Number <> 0 Then
If pt Is Nothing Then
Set pt = .Cells(i, 1)
Else
Set pt = Union(pt, .Cells(i, 1))
End If
Err.Clear
End If
End If
Next
End With
Application.ScreenUpdating=True
pt.EntireRow.Select
End Sub[/code]
===============================================
假設(shè)源文件叫dup.txt,用以下命令行生成過濾后的unique.txt:
copy /y nul unique.txt && for /f "delims=" %a in (dup.txt) do @(findstr /b /e /c:"%a" unique.txt >nul || echo.%a>>unique.txt)
效率不高,如果數(shù)據(jù)量特別大的話,慎用。