added sponge from moreutils

This commit is contained in:
Ville Rantanen
2017-04-04 11:54:37 +03:00
parent dd4c35c28f
commit c4aebbba48
2 changed files with 26 additions and 0 deletions

1
bin/sponge Symbolic link
View File

@@ -0,0 +1 @@
../files/sponge

25
files/sponge Executable file
View File

@@ -0,0 +1,25 @@
#!/usr/bin/env python
import sys
from argparse import ArgumentParser
# Set up options
parser=ArgumentParser(description="Sponge file before writing output: grep foo file.txt | sponge file.txt",
epilog="This tool is a modification from http://joeyh.name/code/moreutils/ with the same name.")
parser.add_argument("-a",action="store_true",dest="append",default=False,
help="Append to file instead of overwriting")
parser.add_argument(type=str,dest="file",
help="File to write to")
options=parser.parse_args()
# Read data
data=sys.stdin.read()
# choose mode
mode="w"
if options.append:
mode="a"
# Write the file
writer=open(options.file, mode)
writer.write(data)
writer.close()