GoLang: Command Line Unique Tool (Read from Keyboard)
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
Reposted to Blog
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Thank you for reading ^^^^^^^^^^^^^^^
NEW! Following my Trail (Upvote or/and Downvote)
Follow me for topics of Algorithms, Blockchain and Cloud.
I am @justyy - a Steem Witness
https://steemyy.com
My contributions
- Video Downloader
- Steem Blockchain Tools
- Free Cryptos API
- VPS Database
- Computing Technology Blog
- A few useless tools
- And some other online software/tools
- Merge Files/Videos
- LOGO Turtle Programming Chrome Extension
- Teaching Kids Programming - Youtube Channel and All Contents
Delegation Service
Important Update of Delegation Service!
Support me
If you like my work, please:
- Buy Me a Coffee, Thanks!
- Become my Sponsor, Thanks!
- Voting for me:
https://steemit.com/~witnesses type in justyy and click VOTE
- Delegate SP: https://steemyy.com/sp-delegate-form/?delegatee=justyy
- Vote @justyy as Witness: https://steemyy.com/witness-voting/?witness=justyy&action=approve
- Set @justyy as Proxy: https://steemyy.com/witness-voting/?witness=justyy&action=proxy
Alternatively, you can vote witness or set proxy here: https://steemit.com/~witnesses
Hello sir @justyy today's following post missed your upvotes as I am an active delegator to you--
https://steemit.com/hive-129948/@rme/photography-of-nature-sky-and-touch-of-urban-civilization
done.
^^ :D Thank You