/* The MIT License (MIT) Copyright (c) 2026 pacman64 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /* To compile a smaller-sized command-line app, you can use the `go` command as follows: go build -ldflags "-s -w" -trimpath dedup.go */ package main import ( "bufio" "errors" "io" "os" ) // Note: the code is avoiding using the fmt package to save hundreds of // kilobytes on the resulting executable, which is a noticeable difference. const info = ` dedup [options...] [file...] DEDUPlicate lines prevents the same line from appearing again in the output, after the first time. Unique lines are remembered across inputs. Input is assumed to be UTF-8, and all CRLF byte-pairs are turned into line feeds by default. All (optional) leading options start with either single or double-dash: -h show this help message -help show this help message ` // errNoMoreOutput is a dummy error whose message is ignored, and which // causes the app to quit immediately and successfully var errNoMoreOutput = errors.New(`no more output`) type stringSet map[string]struct{} func main() { buffered := false args := os.Args[1:] if len(args) > 0 { switch args[0] { case `-b`, `--b`, `-buffered`, `--buffered`: buffered = true args = args[1:] case `-h`, `--h`, `-help`, `--help`: os.Stdout.WriteString(info[1:]) return } } if len(args) > 0 && args[0] == `--` { args = args[1:] } liveLines := !buffered if !buffered { if _, err := os.Stdout.Seek(0, io.SeekCurrent); err == nil { liveLines = false } } err := run(os.Stdout, args, liveLines) if err != nil && err != errNoMoreOutput { os.Stderr.WriteString(err.Error()) os.Stderr.WriteString("\n") os.Exit(1) } } func run(w io.Writer, args []string, live bool) error { files := make(stringSet) lines := make(stringSet) bw := bufio.NewWriter(w) defer bw.Flush() for _, name := range args { if _, ok := files[name]; ok { continue } files[name] = struct{}{} if err := handleFile(bw, name, lines, live); err != nil { return err } } if len(args) == 0 { return dedup(bw, os.Stdin, lines, live) } return nil } func handleFile(w *bufio.Writer, name string, got stringSet, live bool) error { if name == `` || name == `-` { return dedup(w, os.Stdin, got, live) } f, err := os.Open(name) if err != nil { return errors.New(`can't read from file named "` + name + `"`) } defer f.Close() return dedup(w, f, got, live) } func dedup(w *bufio.Writer, r io.Reader, got stringSet, live bool) error { const gb = 1024 * 1024 * 1024 sc := bufio.NewScanner(r) sc.Buffer(nil, 8*gb) for sc.Scan() { line := sc.Text() if _, ok := got[line]; ok { continue } got[line] = struct{}{} w.Write(sc.Bytes()) if w.WriteByte('\n') != nil { return errNoMoreOutput } if !live { continue } if err := w.Flush(); err != nil { return errNoMoreOutput } } return sc.Err() }