https://i.ytimg.com/vi/pLb8AfLbkPo/mqdefault.jpg

Cat loves sponge as you can see in this video, but if it’s soaked with water, a sponge bathing? They would run away next time they see you hiding a sponge behind you. This video provides a nice instructions, if you want to try this one-time only bathing technique:

  1. Acquire cat and remove collar.

    First step is always the hardest, they have sixth sense, they can smell your intentions!

  2. Lightly Soap with dampened cloth (warm diluted solution of cat shampoo is best), ~ praise cat throughout ~

    I doubt they really buy it.

  3. Thoroughly rinse with warm damp cloth, then

  4. Gently towel dry. (do not iron)

    I bet some owners would want to try if ironing is more efficient.

I found it’s always fun to watch cat getting bathed, though there are some cat breeds do like water. And…

Wait a minute!

Why am I posting about cat bathing? Back to the topic, uhm, I mean start the topic.

Someone posted a question on Gentoo Forums about how to do cat testfile > testfile. Using cat as example to ask that question is confusing, check the followings:

# Mistakenly list Mary as John, correcting
sed 's/John/Mary/g' testfile1 > testfile1
# Who doesn't love Mary? So keep Mary only
grep Mary testfile2 > testfile2

If you have ever learned a lesson from above, you would know the files would be empty after. Of course, GNU sed has in-place editing extension, and I think you can use it to do grep job. Anyway, someone answered with about sponge from moreutils collection. With it, you can

sed 's/John/Mary/g' testfile1 | sponge testfile1 #
grep Mary testfile2 | sponge testfile2

Before, I always cp to /tmp, then process the one at /tmp and redirect the result to original file. I don’t think I would really use it because it’s mostly not installed on our system and usually I would avoid in-place processing. First, they don’t save memory, they cache result and output to the file at once, you gain nothing except the coding convenience; second, if something goes wrong with your code—not only the sponge but also outputing to original file, the data file will be all or partially gone. For example,

grep -whatthehell this_file_is | sponge this_file_is #

this_file_is is empty after running with wrong options. One mistake and you test on real data file without backup, you will swear pretty hard.

Even I won’t use it, I still wrote a simple Bash function for similar functionality:

sponges () { lines=() ; while read line; do lines[${#lines[@]}]="$line" ; done ; echo -n '' > "$1" ; for ((i=0;i<${#lines[@]};i++)) ; do echo "${lines[i]}" >> "$1" ; done }

# Break down for readability
sponges () {
  lines=()
  while read line; do
    lines[${#lines[@]}]="$line"
  done
  echo -n '' > "$1"
  for ((i=0;i<${#lines[@]};i++));
    do echo "${lines[i]}" >> "$1"
  done
}

Usage:

sed 's/John/Mary/g' testfile1 | sponges testfile1 #
grep Mary testfile2 | sponges testfile2

This Bash function isn’t good and might have bugs. For example, if you supply with no filename as first argument. Just for fun.

My conclusion is: Sponge bathing is not recommended. :)

Hey…… kitten kitten kitten… Your favorite tuna… (hiding sponge in another hand) where are you?