added sponge from moreutils
This commit is contained in:
1
bin/sponge
Symbolic link
1
bin/sponge
Symbolic link
@@ -0,0 +1 @@
|
||||
../files/sponge
|
||||
25
files/sponge
Executable file
25
files/sponge
Executable 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()
|
||||
Reference in New Issue
Block a user