uniq: -U option is implemented. It is needed for my "profile" file.
This commit is contained in:
parent
15e21be18c
commit
580d70cbe1
2 changed files with 47 additions and 0 deletions
|
@ -14,6 +14,7 @@ import(
|
|||
"github.com/jienfak/goblin/ls"
|
||||
"github.com/jienfak/goblin/yes"
|
||||
"github.com/jienfak/goblin/date"
|
||||
"github.com/jienfak/goblin/uniq"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
@ -34,6 +35,7 @@ func main() {
|
|||
"ls" : ls.Run,
|
||||
"yes" : yes.Run,
|
||||
"date" : date.Run,
|
||||
"uniq" : uniq.Run,
|
||||
}
|
||||
|
||||
if binBase := path.Base(os.Args[0]) ; binBase != "goblin" {
|
||||
|
|
45
uniq/uniq.go
Normal file
45
uniq/uniq.go
Normal file
|
@ -0,0 +1,45 @@
|
|||
/* Yes program implementation. */
|
||||
package uniq
|
||||
|
||||
import(
|
||||
"os"
|
||||
"fmt"
|
||||
"flag"
|
||||
"bufio"
|
||||
"io"
|
||||
)
|
||||
|
||||
func Run(args []string) int {
|
||||
var(
|
||||
Uflag bool
|
||||
)
|
||||
status := 0
|
||||
arg0 := args[0]
|
||||
args = args[1:]
|
||||
flagSet := flag.NewFlagSet(arg0, flag.ExitOnError)
|
||||
flagSet.BoolVar(&Uflag, "U", false, "Print every line just one time.")
|
||||
flagSet.Usage = func() {
|
||||
fmt.Fprintf(os.Stderr, "Usage of %s: %s [options] [string]\n", arg0, arg0)
|
||||
flagSet.PrintDefaults()
|
||||
}
|
||||
flagSet.Parse(args)
|
||||
args = flagSet.Args()
|
||||
|
||||
r := bufio.NewReader(os.Stdin)
|
||||
u := make(map[string]int)
|
||||
if Uflag {
|
||||
for{
|
||||
l, e := r.ReadString('\n')
|
||||
if e==io.EOF {
|
||||
break
|
||||
}
|
||||
_, haskey := u[l]
|
||||
if !haskey {
|
||||
u[l] = 1
|
||||
fmt.Print(l)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return status
|
||||
}
|
Loading…
Reference in a new issue