A sed interpreter written in Golang

A sed interpreter written in Golang.

Most behaviors of this program is implemented follow GNU-sed, except all those operations related to external files are kept unfinished.

the grammar is even more loosen compare to the GNU one: the semicolon ; after each command is not required(except for s). so we can write commands in this style:

$ echo -e 'a\nb\nc' | ./sed -n 'hgNhnxGs/\n//g;s/abc/ABC/p'
ABC

Only two command line parameters are supplied.

$ ./sed -h
Usage of ./sed:
  -n	inhibit printing
  -s	work in pseudo-stream mode

-r is not necessary since ERE is enabled by default, and there's no way to switch it off. As the standard Golang libs use RE2 as the underlying regular expression support, there may exists some subtle differences between these two standards.

-s can be regarded as an extension upon the original version.

basically spreaking, a sed implementation follows the POSIX standard is a line based editing tool. Yes, no matter what the name it is from. It's line based, not stream oriented! one will get into trouble when facing problem to process spanning lines, and that's why N, n, b, t these commands come into play.

think about this problem, if we want to remove all the newline feeds between a <p>, </p> pair. How to do that?

$ echo -e "a\n<p>\nb\nc\n</p>\nd\n<p>\ne\nf\n</p>\ng"
a
<p>
b
c
</p>
d
<p>
e
f
</p>
g

although there's multiple ways to achieve that, but I believe the solution listed below gives us some clue how a typical stream oriented processing way is like.

$ echo -e "a\n<p>\nb\nc\n</p>\nd\n<p>\ne\nf\n</p>\ng" | sed '/<p>/!b;:x N;/<\/p>/!bx;s/\n//g;'
a
<p>bc</p>
d
<p>ef</p>
g
  • step 1. before we find the start pattern, we simplly output everything that we read from stream.
  • step 2. after we find the start pattern, we are in a match range. all the inputs should be buffered until the stop pattern is founded.
  • step 3. actually processing

Using this tool, you can achieve this just like the following:

$ echo -e "a\n<p>\nb\nc\n</p>\nd\n<p>\ne\nf\n</p>\ng" | ./sed -s '/<p>/,/<\/p>/s/\n//g'
a
<p>bc</p>
d
<p>ef</p>
g

the stream mode is also applied to line pattern, and relative range pattern as well

remove all the `\n':

$ echo -e "a\n<p>\nb\nc\n</p>\nd\n<p>\ne\nf\n</p>\ng" | ./sed -s '1,$s/\n//g'
a<p>bc</p>d<p>ef</p>g

you can even omit 1, $. when no address pattern is defined. this tool will take the first and the last line as the range pattern.

$ echo -e "a\n<p>\nb\nc\n</p>\nd\n<p>\ne\nf\n</p>\ng" | ./sed -s 's/\n//g'
a<p>bc</p>d<p>ef</p>g

hope the tool will be helpful, or give someone enlightenment.

any feedback: [email protected]

Similar Resources

Highly configurable prompt builder for Bash, ZSH and PowerShell written in Go.

Highly configurable prompt builder for Bash, ZSH and PowerShell written in Go.

Go Bullet Train (GBT) Highly configurable prompt builder for Bash, ZSH and PowerShell written in Go. It's inspired by the Oh My ZSH Bullet Train theme

Dec 17, 2022

Beagle is a CLI written in Go to search for an specific username across the Internet.

Beagle __ \,--------/_/'--o Use beagle with /_ ___ /~" responsibility. /_/_/ /_/_/ ^^^^^^^^^^^^^^^^^^ Beagle is a CLI w

Nov 16, 2022

A template repository to quickly scaffold a Kubewarden policy written with Go language

go-policy-template This is a template repository that can be used to to quickly scaffold a Kubewarden policy written with Go language. Don't forget to

Sep 7, 2022

A lightweight Vault client module written in Go, with no dependencies, that is intuitive and user-friendly

libvault A lightweight Hashicorp Vault client written in Go, with no dependencies. It aims to provide an intuitive, simple API that is easy to use. Ju

Sep 18, 2022

Airplay 2 Receiver written in go

Go Play 2 This is a work in progress Airplay 2 Speaker implementation largely inspired by airplay2-receiver Status Can be registered with the Home App

Jan 7, 2023

Devtron is an open source software delivery workflow for kubernetes written in go.

Devtron is an open source software delivery workflow for kubernetes written in go.

Devtron is an open source software delivery workflow for kubernetes written in go.

Jan 8, 2023

Little Reversing CrackMe written in GO

Go_CrackMe Little Reversing CrackMe written in GO Info: I created little reversing crackme challenge written in GO language for educational purpose. A

Jul 22, 2022

Medieival Dynasty save backuper written in Go.

Medieival Dynasty save backuper written in Go.

Medieival-Dynasty-save-backup Medieival Dynasty save backupper written in Go. Windows binaries Setup Configure the config file. Option Info interval B

Apr 12, 2022

A package manager written in Go which uses the LFS Symlink method.

pacsym A package manager powered by symlinks. How to use The package manager assumes that all software installed is installed with /usr/pkg/packagena

Dec 11, 2021
Related tags
Rest API for todoapp written in Golang, using clean architecture, CI/CD
Rest API for todoapp written in Golang, using clean architecture, CI/CD

todoapp-backend Rest API for todoapp written in Golang, using Clean Architecture and CI/CD (includes unit tests and integration tests). Using: Web fra

Oct 23, 2022
A Prometheus exporter, written in Golang, for Magento 2

Magento 2 Prometheus Exporter A Prometheus exporter, written in Golang, for Magento 2. Philosophy It might be abnormal to start with the "philosophy"

May 3, 2022
A cross platform CLI for Flyte. Written in Golang. Offers an intuitive interface to Flyte https://flytectl.readthedocs.io/en/latest/
A cross platform CLI for Flyte. Written in Golang. Offers an intuitive interface to Flyte https://flytectl.readthedocs.io/en/latest/

FlyteCTL Flyte's official command-line interface Documentation · Contribution Guide FlyteCTL was designed as a portable and lightweight command-line i

Nov 7, 2022
library for working with github api, written in Golang

gitdata library for working with github api, written in Golang Example: get user data package main import ( "fmt" "log" "github.com/a1excoder/git

May 19, 2022
dashd-go is an alternative full node dash implementation written in Go (golang)

dashd-go is an alternative full node dash implementation written in Go (golang)

Jan 26, 2022
Rainbow pfetch-like program written in golang.

rfetch Rainbow pfetch-like program written in golang. It has an kind of lolcat function called random, maybe someday I will rewrite lolcat to golang,

Jan 30, 2022
Goatmo - Dht22 prometheus exporter written in golang

description Prometheus exporter for the DHT22 written in go. usage In order to a

Aug 8, 2022
Fast cross-platform HTTP benchmarking tool written in Go

bombardier bombardier is a HTTP(S) benchmarking tool. It is written in Go programming language and uses excellent fasthttp instead of Go's default htt

Jan 2, 2023
:rocket: Modern cross-platform HTTP load-testing tool written in Go
:rocket: Modern cross-platform HTTP load-testing tool written in Go

English | 中文 Cassowary is a modern HTTP/S, intuitive & cross-platform load testing tool built in Go for developers, testers and sysadmins. Cassowary d

Dec 29, 2022
A task runner / simpler Make alternative written in Go
A task runner / simpler Make alternative written in Go

Task Task is a task runner / build tool that aims to be simpler and easier to use than, for example, GNU Make. See taskfile.dev for the documentation.

Jan 8, 2023