Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Text.RegularExpressions;
- namespace Filter
- {
- class Program
- {
- static void Main(string[] args)
- {
- if (args.Length < 3)
- {
- Console.WriteLine("Usage: filter input output regex");
- return;
- }
- string input = args[0];
- string output = args[1];
- string regex = args[2];
- if (!File.Exists(input))
- {
- Console.WriteLine("Could not find input file.");
- return;
- }
- using (FileStream read = File.OpenRead(input))
- {
- using (TextReader reader = new StreamReader(read))
- {
- File.WriteAllLines(output, GetLines(reader, regex));
- }
- }
- }
- private static IEnumerable<string> GetLines(TextReader reader, string regex)
- {
- Regex x = new Regex(regex);
- while (true)
- {
- string line = reader.ReadLine();
- if (line != null)
- {
- if (x.IsMatch(line))
- {
- yield return line;
- }
- }
- else
- {
- break;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement