Skip to content

Instantly share code, notes, and snippets.

@vodrazka
Created May 31, 2017 13:23
Show Gist options
  • Save vodrazka/4bc8a11bac3be28f39b21447699a0d94 to your computer and use it in GitHub Desktop.
Save vodrazka/4bc8a11bac3be28f39b21447699a0d94 to your computer and use it in GitHub Desktop.
package main
import (
"bufio"
"os"
"io"
"log"
"fmt"
"flag"
"os/signal"
"syscall"
)
var nthByte int
var replacement int
var buffLines int
func main() {
initFlags()
interrupt()
r := bufio.NewReaderSize(os.Stdin, buffLines*nthByte)
buf := make([]byte, 0, nthByte)
replacementByte := byte(replacement)
for {
n, err := r.Read(buf[:cap(buf)])
buf = buf[:n]
if n < nthByte {
m, err2 := r.Read(buf[n:cap(buf)])
err = err2
buf = buf[:m+n]
}
if n == 0 {
if err == nil {
continue
}
if err == io.EOF {
break
}
log.Fatal(err)
}
buf[nthByte-1] = replacementByte
fmt.Print(string(buf))
}
}
func interrupt() {
c := make(chan os.Signal, 2)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
<-c
os.Exit(1)
}()
}
func initFlags() {
flag.IntVar(&nthByte, "n", 136, "Each nth byte will be replaced in stream")
flag.IntVar(&replacement, "r", 10, "Replacement decimal")
flag.IntVar(&buffLines, "b", 100, "Number of lines read to buffer")
flag.Parse()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment