This is an education project that attempts to reimplement the GNU coreutils in Go. You can find the full manual here: https://www.gnu.org/software/coreutils/manual/coreutils.html
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
go-coreutils/uniq/main.go

57 lines
887 B

package main
import (
"fmt"
"os"
"bufio"
"strings"
"flag"
)
type Opts struct {
IgnoreCase bool
Count bool
}
func ParseOpts() Opts {
var opts Opts
flag.BoolVar(&opts.IgnoreCase, "i", false, "ignore case")
flag.BoolVar(&opts.Count, "c", false, "count occurence")
flag.Parse()
return opts
}
func StringEqual(a string, b string, ignore_case bool) bool {
if ignore_case {
a = strings.ToLower(a)
b = strings.ToLower(b)
}
return strings.Compare(a, b) == 0
}
func main() {
scan := bufio.NewScanner(os.Stdin)
seen_line := ""
seen_count := 0
opts := ParseOpts()
for scan.Scan() {
line := scan.Text()
if !StringEqual(line, seen_line, opts.IgnoreCase) {
if opts.Count {
fmt.Print(seen_count, " ")
}
fmt.Println(line)
seen_line = line
seen_count = 0
} else {
seen_count++
}
}
}