Skip to content

`sed -i` rewritten in Golang. Faster, more stable, and cleaner than shelling out to `sed`.

License

Notifications You must be signed in to change notification settings

carterpeel/gosed

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

56 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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())
  }
}

About

`sed -i` rewritten in Golang. Faster, more stable, and cleaner than shelling out to `sed`.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages