Created
July 18, 2012 02:56
-
-
Save Crisfole/3133834 to your computer and use it in GitHub Desktop.
Helpful linqpad template for file manipulation
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
void Main() | |
{ | |
string fileName = ""; //Whatever the full path to the file in question is | |
var lines = ReadLines(fileName); | |
// Often I apply some filtering/modifying as follows: | |
//.Skip(1) //If you need to skip some number of lines | |
//.Split('\t') //Or ',', or whatever, but only if you want | |
//.GroupBy(line => line[0]) //Again only if you want | |
string.Join("\n", lines.Select(TransformLine)).Dump(); | |
} | |
public static string TransformLine(string[] line) | |
{ | |
//Obviously you'll do something useful here. | |
return string.Join(",", line); | |
} | |
public static IEnumerable<string> ReadLines(string source) | |
{ | |
using (var reader = new StreamReader(source)) | |
{ | |
string line; | |
while ((line = reader.ReadLine()) != null) | |
{ | |
yield return line; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment