排序文本文件 [英] Sorting text file

查看:156
本文介绍了排序文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个txt文件(差不多60 MiB)

  560000100300100201100001000000000000 [...] 
560000100400100201100001000000000000 [ ...]
560000100400200201100001000000000000 [...]
560000100200100201100001000000000000 [...]





但是最后,它是未分类的。



钥匙是:(他们在一起)

  01003,01004, 01004,01002 

  001,001,002,001 

每行以56000开始,然后第一个键,第二个键和行的其余部分。

我尝试使用SORT,它包含在Windows中。它做得非常好,但我需要有我自己的功能,以防SORT不可用。



输出应该先写560001002001。



有什么想法吗?询问你需要的任何东西。



谢谢。

解决方案

不要使用Windowssort.exe。改为使用VB.Net:


  1. 将文件读入VB.Net字符串列表,每行一行

  2. 对列表进行排序
  3. 写回已排序的文件

下面是一个示例程序从MSDN开始,已经为您完成了大部分工作:

 进口系统
进口System.IO
Imports System.Collections

Module Module1

Sub Main()
Dim objReader As New StreamReader(c:\test.txt)
Dim sLine As String =
Dim arrText As New ArrayList()


sLine = objReader.ReadLine()
如果不是sLine没有那么
arrText.Add(sLine)
End If
循环直到sLine没有任何
objReader.Close()

对于每个sLine在arrText中
控制台.WriteLine(sLine)
下一个
Console.ReadLine()
结束小组

结束模块

以下是ArrayList.Sort()的文档:

http://msdn.microsoft.com/en-us/ library / 8k6e334t.aspx



'希望有帮助!


I've this txt file (almost 60 MiB)

560000100300100201100001000000000000[...]
560000100400100201100001000000000000[...]
560000100400200201100001000000000000[...]
560000100200100201100001000000000000[...]

i'm writing an app in vb .net that do some unrelated process with this file.

But at the end, it's unsorted.

The "keys" are: (they're together)

01003, 01004, 01004, 01002

and

001, 001, 002, 001

Every line starts with 56000 then the first key, the the second key and the rest of the line.

I tried to use SORT, that's included with Windows. It does a pretty nice job, but i need to have my own function in case SORT is not available.

The output should write 560001002001 at first.

Any ideas?, ask whatever you need yo know.

Thank you.

解决方案

Don't use the Windows "sort.exe". Use VB.Net instead:

  1. Read file into a VB.Net string list, a line at a time
  2. Sort the list
  3. Write back the sorted file

Here's an example program from MSDN that already does most of the work for you:

Imports System
Imports System.IO
Imports System.Collections

Module Module1

    Sub Main()
        Dim objReader As New StreamReader("c:\test.txt")
        Dim sLine As String = ""
        Dim arrText As New ArrayList()

        Do
            sLine = objReader.ReadLine()
            If Not sLine Is Nothing Then
                arrText.Add(sLine)
            End If
        Loop Until sLine Is Nothing
        objReader.Close()

        For Each sLine In arrText
            Console.WriteLine(sLine)
        Next
        Console.ReadLine()
    End Sub

End Module

Here's the documentation for ArrayList.Sort():

http://msdn.microsoft.com/en-us/library/8k6e334t.aspx

'Hope that helps!

这篇关于排序文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆