#!/usr/local/bin/gawk -f # # Author: Bernhard Kuehl # Universitaet Osnabrueck # Thu Jun 26 18:59:52 MET DST 1997 # File: wc # Description: # wc Nachbau mit awk # Bugs: endet eine Eingabedatei mit einer Zeile, die nicht mit Return abgeschlossen ist, # so ist die Anzahl gezaehlter Zeichen um 1 zu gross # # function usage() { print "wc [-l] [-c] [-w] [files ...]" exit 1 } function version() { print "akw-Version von wc, Version 1.0, Shell-Programmierung SS97" exit 0 } function printEntry(l,w,c,name) { if(!LFLAG && !CFLAG && !WFLAG) { print "\t"l"\t"w"\t"c,name >> "/tmp/wc.tmp" return } ORS="\t" if(LFLAG) print l >> "/tmp/wc.tmp" if(WFLAG) print w >> "/tmp/wc.tmp" if(CFLAG) print c >> "/tmp/wc.tmp" ORS="\n" print name >> "/tmp/wc.tmp" } BEGIN { FS="[ \t\f\r\v]" #FS="[ \t]" CFLAG=LFLAG=WFLAG=lines=words=chars=totalLines=totalWords=totalChars=argind=files=0 filename="" system("rm -f /tmp/wc.tmp") system("touch /tmp/wc.tmp") for ( i = 1; i < ARGC; i ++ ) { if ( ARGV[i] ~ /^-/ ) { if ( ARGV[i] ~ /^-c$/ ) { CFLAG=1 ARGV[i] = "" } else if ( ARGV[i] ~ /^-l$/ ) { LFLAG=1 ARGV[i] = "" } else if ( ARGV[i] ~ /^-w$/ ) { WFLAG=1 ARGV[i] = "" } else if ( ARGV[i] ~ /^--$/ ) { ARGV[i] = "" break } else if ( ARGV[i] ~ /^--version$/ ) { version() } else if ( ARGV[i] != "-" ) usage() } else break } } { # wenn zweimal die gleiche Datei # wc-Aufruf ohne Argumente if((ARGIND != argind) || (filename!=FILENAME)) { if(filename!="") { # nicht beim ersten Mal printEntry(lines, words, chars, filename) totalLines+=lines lines=0 totalWords+=words words=0 totalChars+=chars chars=0 for(i=argind+1;i1) # bei mehr als einer Eingabedatei total-Info ausgeben printEntry(totalLines, totalWords, totalChars, "total") system("cat /tmp/wc.tmp") system("rm -f /tmp/wc.tmp") }