diff src/uq.go @ 0:de451fa0c9cd

golang repository.
author pyon@macmini
date Sat, 01 Oct 2016 11:16:31 +0900
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/uq.go	Sat Oct 01 11:16:31 2016 +0900
@@ -0,0 +1,61 @@
+package main
+
+import (
+    "bufio"
+    "flag"
+    "fmt"
+    "os"
+)
+
+func main() {
+    var c = flag.Bool( "c", false, "count each items" )
+    var l = flag.Bool( "l", false, "count items")
+    flag.Parse()
+
+    files := flag.Args()
+
+    count := make( map[string] int )
+    var lines []string
+
+    // Input
+    if len( files ) == 0 {
+        input := bufio.NewScanner( os.Stdin )
+        for input.Scan() {
+            count[input.Text()]++
+            lines = append( lines, input.Text() )
+        }
+    } else {
+        for _, arg := range files {
+            f, err := os.Open( arg )
+            if err != nil {
+                fmt.Fprintf( os.Stderr, "uq : %v\n", err )
+                continue
+            }
+            input := bufio.NewScanner( f )
+            for input.Scan() {
+                count[input.Text()]++
+                lines = append( lines, input.Text() )
+            }
+            f.Close()
+        }
+    }
+
+    // Output
+    if *l {
+        fmt.Println( len(lines), "->", len(count) )
+        return
+    }
+
+    done := make( map[string] int )
+    for _, line := range lines {
+        if done[line] == 0 {
+            if *c {
+                 fmt.Printf( "%s,\t%d\n", line, count[line] )
+            } else {
+                 fmt.Println( line )
+            }
+        }
+        done[line]++
+    }
+}
+