🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.unix.shell
Article #4597

Re: Gawk v24.7: Why Bother???

#4597
From: Tom Christiansen
Date: Sat, 22 Apr 1995 00:00
73 lines
3295 bytes
In comp.lang.perl, urlichs@smurf.noris.de (Matthias Urlichs) writes:
:NB: (THis is a semi-serious question) Is there anything at all which gawk
:    can do better than Perl5?

(Or even perl4, for that matter.)

Well, I actually can think of one thing: it's sometimes slightly easier 
to type simple one-liners in awk (if it's something awk can do):

  awk '$2 > 10 && $4 ~ /text/'
  perl -ane 'if ($F[1] > 10 && $F[3] =~ /text/) { print }'
      # or simply
  perl -ane 'print if $F[1] > 10 && $F[3] =~ /text/'

  awk '{print $3}'
  perl -lane 'print $F[2]'

  awk '{ sum += $4 } END {print sum}'
  perl4 -lane '{ $sum += $F[3] } print $sum if eof()'
  perl5 -lane '{ $sum += $F[3] } END {print $sum}'

Of course, if's something awk DOESN'T do, than it's slightly easier 
to do them in perl. :-)

  # what's octal value of random char?
  perl -e 'printf "%#o\n", ord(shift)' :

  # strip high bits
  perl -pe 'tr/\200-\377/\000-\177/'

  # find text files
  perl -le 'for(@ARGV) {print if -f && -T}' *

  # trim newsrc
  perl5 -i.old -pe 's/!.*?(\d+)$/! 1-$1/' ~/.newsrc

  # cat a dbmfile
  perl -e 'dbmopen(%f,shift,undef);while(($k,$v)=each%f){print "$k: $v\n"}' /etc/aliases

  # remove comments from C program
  perl5 -0777 -pe 's{/\*.*?\*/}{}gs' foo.c

  # make file a month younger than today, defeating reaper daemons
  perl -e '$X=24*60*60; utime(time(),time() + 30 * $X,@ARGV)' *

  # find first unused uid
  perl5 -le '$i++ while getpwuid($i); print $i'

  # find first unused uid after 100, even with perl4
  perl -le '$i = 100; $i++ while ($x) = getpwuid($i); print $i'

  # detect pathetically insecurable systems
  perl5 -le 'use POSIX; print "INSECURE" unless sysconf(_PC_CHOWN_RESTRICTED)'

Also, it's somewhat harder to write code that so enhances 
(or imperils :-) job-security as it is in perl:

  # display reasonable manpath
  echo $PATH | perl5 -nl -072 -e '
    s![^/+]*$!man!&&-d&&!$s{$_}++&&push@m,$_;END{print"@m"}'

And once you're not limited to one line, the list of things 
that awk is NOT better at than perl gets MUCH longer, as in:

  # copy icky old ndbm database file into nifty new berkeley db format
  perl5 -e 'use POSIX; use NDBM_File; use DB_File;
	    ($n, $o) = @ARGV;
	    tie %o, NDBM_File, $o, 1, 0;
	    tie %n, DB_File,   $n, O_CREAT|O_RDWR;
	    %n = %o;
	  ' old_file_in_ndbm_format new_file_in_berkeley_db_format

--tom
Technical Details
Message-ID: <3nbdqv$cbc@csnews.cs.colorado.edu>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <3mpj9f$d1h@csnews.cs.colorado.edu> <jerry.798231458@tivoli.com> <3n8qt8$shf@smurf.noris.de>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 1020c6,2f1e554b7912769e,start
X-Google-Attributes: gid1020c6,public
X-Google-Thread: 10259a,c6417ab4174dabc8
X-Google-Attributes: gid10259a,public
X-Google-Thread: 1025b4,4df32fd703a6b724
X-Google-Attributes: gid1025b4,public
X-Google-Thread: 1012a6,4df32fd703a6b724
X-Google-Attributes: gid1012a6,public
From: Tom Christiansen <tchrist@mox.perl.com>
Subject: Re: Gawk v24.7: Why Bother???
Date: 1995/04/22
Message-ID: <3nbdqv$cbc@csnews.cs.colorado.edu>
X-Deja-AN: 101361807
references: <3mpj9f$d1h@csnews.cs.colorado.edu> <jerry.798231458@tivoli.com> <3n8qt8$shf@smurf.noris.de>
organization: Perl Consulting and Training
reply-to: tchrist@mox.perl.com (Tom Christiansen)
newsgroups: comp.unix.shell,gnu.misc.discuss,comp.lang.perl,comp.unix.admin
originator: tchrist@mox