Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 

Repository files navigation

The Original

This is a trivial Go implementation of the sponge command from the moreutils collection.

Description

sponge is a command that

  1. Soaks up the entirety of the data on its standard input (until EOF).
  2. Wrings that data out into a destination file.

It is primarily useful along with a data transformation command that does not itself support intelligent "in-place" editing of files. For example, jq.

Here is an example use of sponge along with jq to do in-place formatting of JSON files.

$ FILE=inventory.json

$ jq . "$FILE" | sponge "$FILE"

Why is a special command needed to do this?

Upon first glance, it seems like normal shell output redirection, i.e. jq . "$FILE" >"$FILE" should be sufficient to do this. But that will truncate the file to 0 bytes immediately and there will be nothing for the formatter command to read.

A more capable normal shell approach, (rm -f "$FILE" ; jq . >"$FILE") <"$FILE", is mentioned here. However, that is still not robust if the formatter command itself fails, such as with a parsing error.

Installation

With the Go compiler installed, run:

go get github.com/frou/sponge

The sponge command will now be compiled and located in $GOPATH/bin

About

Command that soaks up all of stdin and then wrings it into a file

Resources

Stars

3 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages