simonkellet 1 year ago
parent 57ab68cc37
commit c35cc37bf7
  1. 5
      go.mod
  2. 2
      go.sum
  3. BIN
      gogrep
  4. 27
      main.go

@ -2,7 +2,10 @@ module go-linux-tools/go-grep
go 1.20
require github.com/fatih/color v1.15.0
require (
github.com/fatih/color v1.15.0
golang.org/x/exp v0.0.0-20230728194245-b0cb94b80691
)
require (
github.com/mattn/go-colorable v0.1.13 // indirect

@ -5,6 +5,8 @@ github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovk
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng=
github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
golang.org/x/exp v0.0.0-20230728194245-b0cb94b80691 h1:/yRP+0AN7mf5DkD3BAI6TOFnd51gEoDEb8o35jIFtgw=
golang.org/x/exp v0.0.0-20230728194245-b0cb94b80691/go.mod h1:FXUEEKJgO7OQYeo8N01OfiKP8RXMtf6e8aTskBGqWdc=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=

BIN
gogrep

Binary file not shown.

@ -1,10 +1,10 @@
package main
import (
"flag"
"fmt"
"io/ioutil"
"os"
"strings"
"github.com/fatih/color"
)
@ -21,16 +21,8 @@ func usage() {
}
func main() {
var flagNoColor = flag.Bool("no-colour", false, "Disable color output")
flag.Parse()
if *flagNoColor {
color.NoColor = true
}
//set up colourful output
found := color.New(color.FgRed).PrintfFunc()
//check no args passed (min. two args need to be passed) and no more than 3
if len(os.Args) <= 2 || len(os.Args) > 3 {
usage() //print usage and then exit
os.Exit(1)
@ -38,22 +30,23 @@ func main() {
search := os.Args[1]
filename := os.Args[2]
fmt.Printf("search: %s\nfilename: %s\n\n", search, filename)
bytes, err := ioutil.ReadFile(filename)
check(err)
word := ""
//split []bytes into words by spaces
line := ""
for i := 0; i < len(bytes); i++ {
word += string(bytes[i])
if string(bytes[i]) == " " {
fmt.Print(word)
word = ""
}
if word == search {
if string(bytes[i]) == "\n" {
line = word
if strings.Contains(line, search) {
found("%s", word)
word = ""
}
line = ""
word = "" //reset line and word
}
}
}

Loading…
Cancel
Save