Tuesday, July 8, 2008

Double Spacing In Awk, Perl and Shell on Linux and Unix

Hey there,

Surprisingly, yesterday's post on multiline spacing with sed on Linux and Unix got some reponse. Enough so, that I figured I'd follow up today and answer the call for equal representation for Perl, awk and the plain old shell (pick one, any one ;)

This post is going to be much the same as yesterday's, except with a few different tricks using a few different languages, rather than just focusing on one (sed). In fact, except for the fact that I'm mentioning sed every fifth word or so, and we're going to kick things off with a brief review of yesterday's simplest command, today should be a sed-free post ;)

First, we'll look at the file itself (appropriately named FILENAME):

host # cat FILENAME
this is the beginning
and now we're in the middle
this is the end

As you recall, from yesterday's post on using sed to handle multiline spacing, we could easily double space this file, using sed, with the following command <-- Note that I won't be repeating the output any more,
since it's the same for every command to follow and we'll assume, from checking out the above mentioned post, that triple-spacing, etc, is an easy concept to grasp once you've got the "double space" down:

host # sed G FILENAME
this is the beginning

and now we're in the middle

this is the end

host #

Awk was the next obvious candidate for "character management." The easiest (read: shortest) way I could figure, find and confirm, was this obvious statement:

host # awk '{print $0"\n"}' FILENAME

I also saw this solution floating around everywhere, but am jailed to a Solaris 8 box as I write. It doesn't work on the native awk, here, but perhaps this does work on Linux
or with Gnu awk. I found that it definitely does work with nawk on Solaris:

host # awk '1;{print ""}'

Perl came up next. It's a heavy hitter, compared to sed and awk, but actually just about as compact on the command line:

host # perl -pe '$_ .= "\n"' FILENAME

Then I decided to check out the shell (which means pretty much every command available) and quickly realized that anything you could really do involved some other basic editing command (or editor) like ed or similar programs. Not really novel. Just an endless parade of variations on the same theme. I decided to just go with this command line, since it's just using the shell and not overtly pulling in any external programs, to get the job done:

host # while read;do echo "$REPLY\n";done<FILENAME

and then this line, too, since (even though it's more convoluted) it's just a little bit shorter:

host # while read x;do echo "$x\n";done<FILENAME

Hopefully that wraps up this whole business with line spacing in files on Linux and Unix. I'm certainly always receptive to additional comments, but, hopefully we've said all that really needs to be said on this topic and can move on away from it tomorrow ;)


, Mike