Mercurial > mercurial > hgweb_golang.cgi
comparison src/uq.go @ 0:de451fa0c9cd
golang repository.
| author | pyon@macmini |
|---|---|
| date | Sat, 01 Oct 2016 11:16:31 +0900 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:de451fa0c9cd |
|---|---|
| 1 package main | |
| 2 | |
| 3 import ( | |
| 4 "bufio" | |
| 5 "flag" | |
| 6 "fmt" | |
| 7 "os" | |
| 8 ) | |
| 9 | |
| 10 func main() { | |
| 11 var c = flag.Bool( "c", false, "count each items" ) | |
| 12 var l = flag.Bool( "l", false, "count items") | |
| 13 flag.Parse() | |
| 14 | |
| 15 files := flag.Args() | |
| 16 | |
| 17 count := make( map[string] int ) | |
| 18 var lines []string | |
| 19 | |
| 20 // Input | |
| 21 if len( files ) == 0 { | |
| 22 input := bufio.NewScanner( os.Stdin ) | |
| 23 for input.Scan() { | |
| 24 count[input.Text()]++ | |
| 25 lines = append( lines, input.Text() ) | |
| 26 } | |
| 27 } else { | |
| 28 for _, arg := range files { | |
| 29 f, err := os.Open( arg ) | |
| 30 if err != nil { | |
| 31 fmt.Fprintf( os.Stderr, "uq : %v\n", err ) | |
| 32 continue | |
| 33 } | |
| 34 input := bufio.NewScanner( f ) | |
| 35 for input.Scan() { | |
| 36 count[input.Text()]++ | |
| 37 lines = append( lines, input.Text() ) | |
| 38 } | |
| 39 f.Close() | |
| 40 } | |
| 41 } | |
| 42 | |
| 43 // Output | |
| 44 if *l { | |
| 45 fmt.Println( len(lines), "->", len(count) ) | |
| 46 return | |
| 47 } | |
| 48 | |
| 49 done := make( map[string] int ) | |
| 50 for _, line := range lines { | |
| 51 if done[line] == 0 { | |
| 52 if *c { | |
| 53 fmt.Printf( "%s,\t%d\n", line, count[line] ) | |
| 54 } else { | |
| 55 fmt.Println( line ) | |
| 56 } | |
| 57 } | |
| 58 done[line]++ | |
| 59 } | |
| 60 } | |
| 61 |
