view src/uq.go @ 46:6ec28d3c3e00

small changes.
author pyon@macmini
date Sat, 18 Apr 2020 21:10:29 +0900
parents de451fa0c9cd
children
line wrap: on
line source

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]++
    }
}