GoLang: Command Line Unique Tool (Read from Keyboard)

in #golang5 years ago

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

Delegation Service

Important Update of Delegation Service!

  • Delegate 1000 to justyy: Link
  • Delegate 5000 to justyy: Link
  • Delegate 10000 to justyy: Link

Support me

If you like my work, please:

  1. Delegate SP: https://steemyy.com/sp-delegate-form/?delegatee=justyy
  2. Vote @justyy as Witness: https://steemyy.com/witness-voting/?witness=justyy&action=approve
  3. 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

Sort:  

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

^^ :D Thank You

Coin Marketplace

STEEM 0.04
TRX 0.32
JST 0.084
BTC 60423.20
ETH 1583.38
USDT 1.00
SBD 0.42