Amazon.com Widgets

Safer in-place edit?

Sometimes I need to edit a file in batch mode. Say you have /etc/default/foo and you want to change BAR="-c5" to BAR="-c4". One of the popular methods is using sed in-place edit, like this:

sed -i -e 's/BAR="-c5"/BAR="-c4"/' foo

If your version of sed is antiquated and doesn’t support in-place editing you can do the same using perl:

perl -i -p -e 's/BAR="-c5"/BAR="-c4"/' foo

The problem with this approach is that if the needle doesn’t exist, sed and perl will silently skip doing the replacement. For example if upstream changes BAR="-c5" to BAR="-c3" then your sed/perl will happily and silently leave it at -c3 instead of changing to -c4 and most probably you will not notice until much later.

One can use patch instead, but it is somewhat clumsy to save a diff file with at least 5 lines in it, just to change one character in the input file…

There’s always the possibility to add some more perl code and make it:

perl -i -pe 's/BAR="-c5"/BAR="-c4"/ and $e|=1; END{exit(!$e)}' foo

I feel this is hackish and somewhat error prone though.

What’s your solution?

Popularity: 3% [?]

Genderplot by last.fm

genderplot

Popularity: 18% [?]

Mechanism and function of humor

Alastair Clarke explains:

The theory is an evolutionary and cognitive explanation of how and why any individual finds anything funny. Effectively, it explains that humour occurs when the brain recognizes a pattern that surprises it, and that recognition of this sort is rewarded with the experience of the humorous response, an element of which is broadcast as laughter.

The theory further identifies the importance of pattern recognition in human evolution:

An ability to recognize patterns instantly and unconsciously has proved a fundamental weapon in the cognitive arsenal of human beings. The humorous reward has encouraged the development of such faculties, leading to the unique perceptual and intellectual abilities of our species.

http://www.eurekalert.org/pub_releases/2008-06/ph-maf062708.php

Popularity: 26% [?]