gosed

package module
v0.0.0-...-6eb22a6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 9, 2022 License: MIT Imports: 8 Imported by: 0

README

gosed

sed -i written in Golang as an importable module. This is very useful for replacing specific strings of text in massive files, because sometimes ingesting someBigAssFile.txt into memory isn't a great idea.

Sequential Replacer Usage

package main
import (
  "github.com/carterpeel/gosed"
  "log"
)
func main() {
  // Creates a new replacer type with the provided file 
  replacer, err := gosed.NewReplacer("hugeAssFile.txt");
  if err != nil {
    log.Fatal(err.Error())
  }
  // Creates a new old:new string mapping 
  if err := replacer.NewStringMapping("oldString", "newString"); err != nil {
    log.Fatal(err.Error())
  }
  // Creates a new old:new byte sequence mapping 
  if err := replacer.NewMapping([]byte("oldString2"), []byte("newString2")); err != nil {
    log.Fatal(err.Error())
  }
  
  
  // Replace() Executes a SEQUENTIAL replace operation, meaning a temporary file is allocated for each
  // old:new mapping (slower, less CPU intensive)
  
  // Keep in mind this iterates through the mappings in order, so newly replaced byte sequences can 
  // potentially be replaced by the next old:new mapping, but only if they match.
  if _, err := replacer.Replace(); err != nil {
    log.Fatal(err.Error())
  }
}

Chained Replacer Usage

package main
import (
  "github.com/carterpeel/gosed"
  "log"
)
func main() {
  // Creates a new replacer type with the provided file 
  replacer, err := gosed.NewReplacer("hugeAssFile.txt");
  if err != nil {
    log.Fatal(err.Error())
  }
  // Creates a new old:new string mapping 
  if err := replacer.NewStringMapping("oldString", "newString"); err != nil {
    log.Fatal(err.Error())
  }
  // Creates a new old:new byte sequence mapping 
  if err := replacer.NewMapping([]byte("oldString2"), []byte("newString2")); err != nil {
    log.Fatal(err.Error())
  }
  
  
  // Replace() Executes a CHAINED replace operation, meaning the readers are chained in order
  // and only need to allocate a single temporarily file. (faster, more CPU intensive)
  
  // Keep in mind this iterates through the mappings in order, so newly replaced byte sequences can 
  // potentially be replaced by the next old:new mapping, but only if they match.
  if _, err := replacer.ReplaceChained(); err != nil {
    log.Fatal(err.Error())
  }
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DoChainReplace

func DoChainReplace(rp *Replacer) (int, error)

DoChainReplace does the replace operation with reader chaining, which is faster but more resource intensive.

func DoSequentialReplace

func DoSequentialReplace(rp *Replacer) (int, error)

DoSequentialReplace does the replace operation without reader chaining, which is slower but less resource intensive.

Types

type Replacer

type Replacer struct {
	Config *replacerConfig
}

Replacer contains all of the methods needed to properly execute replace operations

func NewReplacer

func NewReplacer(fileName string) (*Replacer, error)

NewReplacer returns a new *Replacer type

func (*Replacer) NewMapping

func (rp *Replacer) NewMapping(oldString, newString []byte) error

NewMapping maps a new oldString:newString []byte entry

func (*Replacer) NewStringMapping

func (rp *Replacer) NewStringMapping(oldString, newString string) error

NewStringMapping maps a new oldString:newString string entry

func (*Replacer) Replace

func (rp *Replacer) Replace() (int, error)

Replace does the replace operation with a concurrent (sequential) reader --> tmpfile model

func (*Replacer) ReplaceChained

func (rp *Replacer) ReplaceChained() (int, error)

ReplaceChained does the replace operation with a chained reader model

func (*Replacer) Reset

func (rp *Replacer) Reset() error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL