Split text file into smaller multiple text file using command line

后端 未结 9 2130
借酒劲吻你
借酒劲吻你 2020-12-22 18:03

I have multiple text file with about 100,000 lines and I want to split them into smaller text files of 5000 lines each.

I used:

split -l 5000 filena         


        
9条回答
  •  执笔经年
    2020-12-22 18:18

    here is one in c# that doesn't run out of memory when splitting into large chunks! I needed to split 95M file into 10M x line files.

    var fileSuffix = 0;
    int lines = 0;
    Stream fstream = File.OpenWrite($"{filename}.{(++fileSuffix)}");
    StreamWriter sw = new StreamWriter(fstream);
    
    using (var file = File.OpenRead(filename))
    using (var reader = new StreamReader(file))
    {
        while (!reader.EndOfStream)
        {
            sw.WriteLine(reader.ReadLine());
            lines++;
    
            if (lines >= 10000000)
            {
                  sw.Close();
                  fstream.Close();
                  lines = 0;
                  fstream = File.OpenWrite($"{filename}.{(++fileSuffix)}");
                  sw = new StreamWriter(fstream);
            }
        }
    }
    
    sw.Close();
    fstream.Close();
    

提交回复
热议问题