Differences between sponge and tee
No one of them soak up stderr; only stdout.
'tee' writes stdin on stdout and files.
'sponge' writes stdin only on a file; without errors, no output.
(i.e: Unlike 'tee', 'sponge' doesn't write on stdout).
Besides,
"sponge soaks up all its input before opening the output file"
(from its manual)
This difference between them is extremely relevant: 'tee' "reads a byte", and "writes that byte"; 'sponge' waits to receive all the input, and then, writes it.
How can I use a file in a command and redirect output to the same file without truncating it?
You cannot do that because bash processes the redirections first, then executes the command. So by the time grep looks at file_name, it is already empty. You can use a temporary file though.
#!/bin/sh
tmpfile=$(mktemp)
grep -v 'seg[0-9]\{1,\}\.[0-9]\{1\}' file_name > ${tmpfile}
cat ${tmpfile} > file_name
rm -f ${tmpfile}
like that, consider using mktemp
to create the tmpfile but note that it's not POSIX.
How can I convert tabs to spaces in every file of a directory?
Warning: This will break your repo.
This will corrupt binary files, including those under
svn
,.git
! Read the comments before using!
find . -iname '*.java' -type f -exec sed -i.orig 's/\t/ /g' {} +
The original file is saved as [filename].orig
.
Replace '*.java' with the file ending of the file type you are looking for. This way you can prevent accidental corruption of binary files.
Downsides:
- Will replace tabs everywhere in a file.
- Will take a long time if you happen to have a 5GB SQL dump in this directory.
Unix: How can I prepend output to a file?
sed will happily do that for you, using -i
to edit in place, eg.
sed -i -e "1i `date "+%Y-%m-%d at %H:%M"`" some_file
How to substitute shell variables in complex text files
Looking, it turns out on my system there is an envsubst
command which is part of the gettext-base package.
So, this makes it easy:
envsubst < "source.txt" > "destination.txt"
Note if you want to use the same file for both, you'll have to use something like moreutil's sponge
, as suggested by Johnny Utahh: envsubst < "source.txt" | sponge "source.txt"
. (Because the shell redirect will otherwise empty the file before its read.)
Unix command to prepend text to a file
sed -i.old '1s;^;to be prepended;' inFile
-i
writes the change in place and take a backup if any extension is given. (In this case,.old
)1s;^;to be prepended;
substitutes the beginning of the first line by the given replacement string, using;
as a command delimiter.
sudo echo something /etc/privilegedFile doesn't work
Use tee --append
or tee -a
.
echo 'deb blah ... blah' | sudo tee -a /etc/apt/sources.list
Make sure to avoid quotes inside quotes.
To avoid printing data back to the console, redirect the output to /dev/null.
echo 'deb blah ... blah' | sudo tee -a /etc/apt/sources.list > /dev/null
Remember about the (-a
/--append
) flag!
Just tee
works like >
and will overwrite your file. tee -a
works like >>
and will write at the end of the file.
Related Topics
Linux Script with Netcat Stops Working After X Hours
Tar Command Changing The Owner:Group While Extracting
Ffmpeg Concat and Scale Simultaneously
Compile Swift Code to Native Executable for Linux
How to Pass Local Variable to Remote Using Ssh and Bash Script
Using Linux Virtual Mouse Driver
Haskell Cabal: Mysterious Missing or Recursive Dependencies
Undefined Reference to Symbol 'Timer_Settime@@Glibc_2.3.3
Why Doesn't Time() from Time.H Have a Syscall to Sys_Time
How to Speed Up a Video by Dropping Frames
How to Paste from Buffer in Ex Mode of Vim
One-Shot *Level*-Triggered Epoll(): Does Epolloneshot Imply Epollet
Naming Convention for Posix Flags
I Would Like to Store All Command-Line Arguments to a Bash Script into a Single Variable