one-liners
Michael Fowler
wolfm at pobox.alaska.net
Wed Feb 2 03:16:32 CST 2000
I just had a few one-liners I found useful as of late, and wanted to share.
Recently, I decided to change from 8-space tabs using a tab character to
4-space tabs using spaces. Needing to convert some pre-existing code I was
editing, I wrote a small script. The script in actually written in sh, and
does the following (basically):
mv $1 $1.bak
expand $1.bak | unexpand | expand --initial -t 4 > $1
Given a filename as an argument, it renames the file, converts all tabs into
8 spaces (using tabstops, so alignment is kept), then converts all leading
whitespace to tabs (thus internal whitespace is maintained), then
re-converts any leading tabs to 4 spaces. The extra steps are required to
maintain formatting; often I use tabs to line things up, e.g.
my $foo = "bar";
my $bar = "foo";
If those are tab characters, and all tabs are arbitrarily converted to 4
spaces, things no longer properly line up.
Here's the Perl equivalent:
perl -MText::Tabs -i.bak -pnwe '$tabstop = 8; $_ = expand($_);
s/^(\s+)/unexpand($1)/e; $tabstop = 4; $_ = expand($_);' filename
This has the advantage of being able to process multiple files listed on the
command-line, with no extra work. The shell script, on the other hand,
would require modification.
Recently, I was messing around with Apache logfiles, and needed to check
what domains were in a given logfile.
perl -anwe '$domains{$F[1]}++; END { while (my($domain, $count) =
each(%domains)) { printf("%4d %s\n", $count, $domain) } }'
This prints out a list of the domains in the logfile, and how many
occurrences there were.
Michael
--
Administrator www.shoebox.net
Programmer, System Administrator www.gallanttech.com
--
=================================================
Mailing list info: If at any time you wish to (un|re)subscribe to
the list send the request to majordomo at hfb.pm.org. All requests
should be in the body, and look like such
subscribe anchorage-pm-list
unsubscribe anchorage-pm-list
More information about the Anchorage-pm
mailing list