Skip to content

Instantly share code, notes, and snippets.

@Crisfole
Created July 18, 2012 02:56
Show Gist options
  • Save Crisfole/3133834 to your computer and use it in GitHub Desktop.
Save Crisfole/3133834 to your computer and use it in GitHub Desktop.
Helpful linqpad template for file manipulation
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