J
J
Javasavr2019-09-12 12:23:41
go
Javasavr, 2019-09-12 12:23:41

How to find the line containing ^ and remove it?

There is a data.txt file, it contains lines containing the ^ symbol, I need to find all the lines containing this symbol and delete them, tell me how to implement this

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladislav, 2019-09-12
@Javasavr

spoiler
package main

import (
  "bufio"
  "fmt"
  "io"
  "log"
  "os"
  "strings"
)

func main() {
  r, err := os.Open("data.txt")
  if err != nil {
    log.Fatal(err)
  }
  defer r.Close()

  s, err := excludeLines(r)
  if err != nil {
    log.Fatal(err)
  }

  fmt.Println(s)
}

func excludeLines(r io.Reader) (string, error) {
  var lines []string
  s := bufio.NewScanner(r)
  for s.Scan() {
    text := s.Text()

    if strings.Contains(text, "^") {
      continue
    }

    lines = append(lines, text)
  }

  err := s.Err()
  if err != nil {
    return "", errors.Wrap(err, "Scan")
  }

  return strings.Join(lines, "\n"), nil
}

https://play.golang.org/p/azNC8PXgonI

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question