Golang

sedplus – Easier SED tool for Mac, Linux and Windows


SED is a powerful stream editor if you can get the syntax right. It was originally created in 1973 by Lee McMahon of Bell Labs as one of the first tools to support Regular Expression (RegEx) and appears in many bash scripts today. sedplus isn’t trying to replace everything that sed does, but as an alternative for some of the basic routine cases in an easier to read syntax.

Lets take a look at a simple replacement in sed:

cat config | sed 's/apple/orange/g'

but what if that could turn to:

cat config | sedplus --find apple --replace orange

It does take up more space, but is easier to read what it is doing. Now you can still probably parse what the first sed command is doing, but in the next example, lets throw in some symbols and some regex to see how readable it is.

A common task is the need to update a config file, and here is where RegEx can help and make everything more complicated:

sed 's/  url:.+/url:http:\/\/192.168.1.3\/auth/'

Now once you add in the escape slashes, the sed command starts to blur. The nice thing with the separate find-line to select the entire url line without having to regex wildcard, and a separate replace where you don’t have to escape anything. Plus, a flag to detect if the line doesn’t exist.

sedplus --find-line "url:" --replace "  url: http://192.168.1.3/auth" --error-if-not-found

Installing sedplus

If you are on a mac, just run

brew install szazeski/tap/sedplus

Or check out https://github.com/szazeski/sedplus for linux and windows install code. While you are there check out the code or even contribute to the project. My goal is to make common tasks you do today with sed that require looking up how to do, just simple flags in sedplus.

--lowercase converts text to lower ( SED -> sed )

--uppercase convert text to upper ( sed -> SED )

--find 'string-to-find'

--find-line 'ip:' finds a line containing the string and replace the whole line

--replace 'new-string'

--error-if-not-found – if the find string is not found, exit with an error code otherwise it will return exit 0.

--case-insensitive – case insensitive find

--trim removes whitespace from the start and end of each line

--numeric remove all characters except 0-9 ( asd1234 -> 1234 )

--alpha remove all characters except a-zA-Z ( asdf1234 -> asdf )

--alphanumeric remove all symbols

--compact-whitespace reduce all spacing to a single space (processed last)

--remove-quotes

--remove-time

--remove-date

--remove-ip

--remove-uuid

Check out this other project…

Of course, once you start working on a project, you will find someone else had your same idea and is much further along than you. 😀

sttr – string translate
brew tap abhimanyu003/sttr
brew install sttr

openanalytics 29926 views

I'm a 35 year old UIUC Computer Engineer building mobile apps, websites and hardware integrations with an interest in 3D printing, biotechnology and Arduinos.


View Comments
There are currently no comments.

This site uses Akismet to reduce spam. Learn how your comment data is processed.