[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: OT - trivial programming language



On Fri, 21 May 2004 14:55:35 -0400
richard lyons <richard@the-place.net> wrote:

> I'm asking for a bit of advice here.  
> 
> I wish to convert a kaddressbook database to abook format saving as 
> many fields as possible.  
> 

[ ... ]

> 
> I could probably do it in perl - but I've never really learned perl, 
> and would have to work from the manual.
> 

Perl is a great language.  I think it can solve many issues quickly, but it does appear large and overwhelming at first.

> 
> I really do need to equip myself with a convenient scripting language 
> for all these day-to-day admin tasks, and I'd like it if it can do a 
> little maths for me at time too.  Please advise me which manual to 
> open.
> 

Until this past year, I used three primary tools in conjunction with the Bourne Shell for my "day-to-day admin tasks": sed, grep and awk.  With these three tools you can manipulate text to your heart's content.  In this case, if I didn't want to use perl, awk is a good choice.  For example,

echo "hanson,carlos,email@here.com,123 street,somewhere,555-1234" |
awk '
  BEGIN { FS="," }
  {
    printf ("[%s]\n", NR)
    printf ("name=%s %s\n", $2, $1)
    printf ("email=%s\n", $3)
    printf ("address=%s\n", $4)
    printf ("city=%s\n", $5)
    printf ("phone=%s\n", $6)
    printf ("\n")
  }'

but the equivalent in perl script is

#!/usr/bin/perl
my $count = 1;
while (<>) {
    chomp;
    my @record = split(/,/);
    printf ("[%s]\n", $count++);
    printf ("name=%s %s\n", $record[1], $record[0]);
    printf ("email=%s\n", $record[2]);
    printf ("address=%s\n", $record[3]);
    printf ("city=%s\n", $record[4]);
    printf ("phone=%s\n", $record[5]);
    printf ("\n");
}

If I was to do something quick on the command line, I would use sed, grep and awk.  Otherwise, I try to use perl.  Perl is a combination of those tools and more.

Enjoy.

-- 
Carlos Hanson
Webmaster and Postmaster
Tigard-Tualatin School District

ph: 503.431.4053



Reply to: