The linux uniq filters adjacent duplicate lines and then output to standout. Most of the times, we want only the unique lines, and thus we have to combine the uniq command with sort. We can use GoLang to implement a uniq tool that will only output the unique entries to stdout from stdin.
We use the bufio.NewReader to construct a reader from os.Stdin. Then, we can read a string/line from stdin using ReadString(‘\n’). Then, we use strings.Trim(s, ‘\n’) to trim the line-return. And we put the line into a hash map.
When the line is empty – we iterate over the map using := range syntax and print out the keys (unique lines).
package main
import (
"fmt"
"bufio"
"os"
"strings"
)
func main() {
reader := bufio.NewReader(os.Stdin)
var data = make(map[string]bool)
for true {
s, _ := reader.ReadString('\n')
s = strings.Trim(s, "\n")
if len(s) == 0 {
break
}
data[s] = true
}
for a, _ := range data {
fmt.Println(a)
}
}
For example – combined with the Full Permutation tool:
$ ./perm.sh 112 | go run uniq.go
112
121
211
–EOF (The Ultimate Computing & Technology Blog) —
249 wordsLast Post: Teaching Kids Programming - Number of Changing Directions
Next Post: Teaching Kids Programming - Dynamic Programming Algorithms to Compute the Maximum Non-Adjacent Tree Sum