I should have mentioned in my previous blog post that my friend
Jonathan
originally told me about perl -00pe0
. After I posted it, we
exchanged a few more emails about various ways to squeeze multiple
blank lines, and he pointed out that cat -s
is not, in fact,
exactly the same as perl -00pe0
. The difference is how they deal
with leading blank lines. cat -s
will squeeze them down to a single
blank line, but perl -00pe0
will remove all of the leading blank
lines.
Perl One-liner to Squeeze Blank Lines
I learned a fun new perl one-liner today:
perl -00pe0 foo >bar
What does it do? Well, it’s nearly the same as cp foo bar
, except
that it squeezes multiple blank lines into foo
into a single blank
line in bar
.
How does it do that? Well, let’s break it down piece by piece.
Perl’s -0
flag lets you change perl’s “input record separator”.
This lets you fiddle with perl’s idea of what a “line” is. Normally
perl reads in text a line at a time, where a “line” is a string of
text terminated by a newline character. But when you set the -0
flag to 0
, perl will keep reading until it hits a string of 2 or
more newlines in a row. In other words, perl will read in text a
paragraph at a time, and stop when it hits a blank line. If there are
multiple blank lines, perl will treat them as if there was just a
single blank line and throw away the extras. (You can also do the
same thing by setting perl’s special $/ variable to “”.)
The next flag is -p
. That tells perl to read in text a “line” at a
time, execute some code against each line, and then print the line to
standard output. Of course, because of the -00
right before it,
each “line” is actually a paragraph.
The final flag is -e
. This tells perl that the following parameter
is a program that it should execute after it reads in each line.
Normally this is given in between single quotes, but here the entire
program is simply “0”. This is a perl idiom for a no-op. This is
because we don’t need any additional code — the combination of
the -00
and -p
flags are all we need.
Incidentally, blank lines can be squeezed much simply, and with far
less obfuscation, by running cat(1) with the -s
flag:
cat -s foo bar
Why Mawode?
Why did I pick “mawode.com” for my domain name? It goes back to when I visited my friends Jonathan and Chyun in Taipei back in 1998. While I was there they decided I needed to have a proper Chinese name. Most Chinese names are 3 syllables (think Mao Tse-tung, Sun Yat-Sen, and Yo-Yo Ma) and that’s what they were trying to come up with. After agonizing over this for several days, they finally came up with 馬沃德, which is written in Pinyin as Mǎ Wò-Dé. I strung them together to come up with “Mawode”.
In Chinese the usual practice is to say the surname first, so Mǎ is my “last name” and Wò-Dé is my “first name”. Mǎ means “horse”, and is a very common Chinese surname. It’s the same last name as Yo-Yo Ma (although he follows Western practice and puts it at the end). The character 馬 is supposed to be a stylized picture of a horse. If you look closely you can make out the 4 legs at the bottom.
They picked Mǎ because it was the first two letters of “Mankowski”. They tried to do the same thing with “Walt”, but couldn’t get it quite right. Although there are several different meanings for “wa” depending on the tone, none of them really worked for a name. As for the “lt”, well, Chinese doesn’t really have the same “l” sound we have in English, so that was never going to work out, either. They decided on “wò”, meaning “fertile” or “rich”, and “dé”, meaning “morality” or “virtue”. When you say the two syllables together, it sounds pretty close to “Walt”!