0

I have a text file with about 3000 lines. Some lines start with numbers and some with text. For example:

Lines that start with numbers:

001 some text

0017 some text

8 some text...

Lines that start with text:

some text,

some text...

I want to copy lines that start with numbers to result.text.

Lines that start with text to log.txt.

Thanks a lot.

Luis Vito
  • 63
  • 1
  • 5
  • 10
  • 3
    This is plain simple. Show us something you have tried and then we might help you out with your homework. – Karolis Kajenas May 22 '16 at 17:47
  • For this read and write in another file. – Akram Khan May 22 '16 at 17:49
  • {string line; String Report = Char.IsDigit; //not working using (StreamReader reader = new StreamReader("file.txt")) using (StreamWriter writer = new StreamWriter("newfile.txt")) while (reader.ReadLine() != null) { if (reader.ReadLine().StartsWith(Report)) { //writes/starts a new line beginning with ReportRequest writer.WriteLine(line); }} – Luis Vito May 22 '16 at 17:52
  • @LuisVito - please edit your question. Code doesn't go into comments – ChrisF May 22 '16 at 17:55
  • Check [this](http://stackoverflow.com/questions/8037070/whats-the-fastest-way-to-read-a-text-file-line-by-line) out to learn how to read line by line. After that, just check the first character from each of those lines. If it's a digit (use Char.IsDigit Method), write it to a file, if it's not, write it to the other. You can do the research on how to write to files yourself. – Gabriel Em May 22 '16 at 17:58

2 Answers2

1

It can be done in one statement for one file

WriteAllLines("new file", 
    ReadLines("source file")
    .Where(line => line.Lenth > 0 && Char.IsDigit(line[0]))
);

Note that it works on a line-by-line basis internally, i.e. there will be only one line at a time in memory. One line will be read from input processed and written to the output, then the next line and so on. This is because these methods work with IEnumerable<string>. IEnumerable does not buffer the whole file as would be the case if you read the file into an array, for instance.


You would have to repeat this twice for the two output files. Therefore I suggest the following approach, which reads the input file only once:

using (var log = CreateText("log.txt"))
using (var result = CreateText("result.txt")) {
    foreach (string line in ReadLines("source file")) {
        if (line.Length > 0) {
            if (Char.IsDigit(line[0])) {
                result.WriteLine(line);
            } else {
                log.WriteLine(line);
            }
        }
    }
}

This second approach does not buffer the file either.

Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
0

Okay here is how i would do it

  • use File to read your text file into a string[]
  • create 2 string Lists to save "results" and "logs"
  • for each string in the string array get the first character with charAt
  • use Char.IsNumber to know to what List you should Add() the line you are working on
  • use File again to write the Lists to different files

this is enough of a hint incase this is your homework. do your research!

downrep_nation
  • 432
  • 1
  • 7
  • 16