Created
February 7, 2012 09:40
-
-
Save onlytiancai/1758723 to your computer and use it in GitHub Desktop.
linq版本grep
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static void Main(string[] args) | |
{ | |
if (args.Length != 2) return; | |
string searchPattern = args[0], dir = Path.GetDirectoryName(args[1]), filePattern = Path.GetFileName(args[1]); | |
var files = from file in Directory.EnumerateFiles(dir, filePattern, SearchOption.AllDirectories) | |
from line in File.ReadLines(file).Select((text, i) => new { Text = text, Index = i }) | |
where new Regex(searchPattern).IsMatch(line.Text) | |
select new { File = file, LineText = line.Text, LineNo = line.Index }; | |
Parallel.ForEach(files, (f) => { Console.WriteLine("{0}({1}){2}", f.File, f.LineNo, f.LineText); }); | |
} |
LeoShi
commented
Feb 7, 2012
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment