Skip to content

Instantly share code, notes, and snippets.

@brianmed
Created March 28, 2018 03:20
Show Gist options
  • Save brianmed/56e3162dc63885d97a816a6e412fcdfe to your computer and use it in GitHub Desktop.
Save brianmed/56e3162dc63885d97a816a6e412fcdfe to your computer and use it in GitHub Desktop.
dotnet script for sorting du output.. yay
#! "netcoreapp2.0"
#r "nuget: NetStandard.Library, 2.0.0"
# [bpm@Orfgum] c:~/playground/c>sudo du -k ~ | dotnet script ./duSort.csx > joy
using System;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using (var stdin = new StreamReader(Console.OpenStandardInput()))
{
string line;
UInt64 lineNo = 1;
var sizes = new Dictionary<string, long>();
while((line = stdin.ReadLine()) != null) {
try {
var match = Regex.Match(line, @"^(?<bytes>\d+)\s+(?<path>\S.*)");
var groups = match.Groups;
// Console.WriteLine($"{groups["bytes"]} - {groups["path"]}");
sizes.Add(groups["path"].ToString(), Int64.Parse(groups["bytes"].ToString()));
} catch (Exception e) {
Console.WriteLine($"Issue: {e.Message}: {lineNo}");
} finally {
++lineNo;
}
}
foreach (var item in sizes.OrderByDescending(key => key.Value)) {
Console.WriteLine($"{item.Key} - {item.Value}");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment