Path: utzoo!utgpu!jarvis.csri.toronto.edu!rutgers!tut.cis.ohio-state.edu!ucbvax!astroatc.UUCP!tenaglia From: tenaglia@astroatc.UUCP (Chris Tenaglia) Newsgroups: comp.lang.icon Subject: (none) Message-ID: <8908311252.AA28882@astroatc.UUCP> Date: 31 Aug 89 12:52:07 GMT Sender: daemon@ucbvax.BERKELEY.EDU Distribution: inet Organization: The Internet Lines: 208 Dear Icon-group, Here's another of my favorite and handy utilities. I include the instructions first, then the code. There is a variable called 'os' which kind of tells it how to behave on different platforms. It works best in unix and vms. In DOS it may run short of memory (I use Icon 5.9 in DOS because it uses less memory). Chris Tenaglia Astronautics Corporation of America 4115 N. Teutonia Avenue Milwaukee, Wi 53209 USA (414) 447-8200 X-421 USAGE AND DESCRIPTION OF THE IAWK/IGREP UTILITY INTRODUCTION : This utility was written because of something I observed in the creation of many file mungers/analyzers using the icon language. There were all the same program except for a couple of lines that accomplished the actual filtering or editing. So why should one rewrite the same program over and over again, when the same job could be done by a computer? So that is what IAWK/IGREP is. Actually that is two different names for the same program. 2 different names were chosen because the program is used basically 2 different ways. AWK is a unix utility used to gross global conversions on files. It reads one file and writes another. The AWK language file supplies a series of expressions and rules that perform the filtering of the data file. It is a nonprocedural language. GREP is a directory/file scanning utility. A byte stream is run through it, and a 'regular expression' is beat against the stream. And when the regular expression is matched, the stream record is displayed. These are useful but limitted. The regular expression is a powerful, but nonprocedural construct. Therefore, what about such a utility that used icon expressions? They can be more precise, and do more complex things. And so this is just what was done. When run with only one filespec, the program behaves like GREP, simply displaying the analysis. When run with two filespecs, it is running like AWK, generating the second filespec as the output file. USAGE : USAGE: iawk infile [outfile] Once invoked, you will be prompted for a series of expressions. The prompting will stop after the first empty line. This signals the end of the expression entry phase. The expressions should be icon language expressions. They are inserted into a program called grepawk.icn. Its basic form is as follows : global MOST HANDY VARIABLES procedure main() INITIALIZE GLOBAL VARIABLES while write(update(read())) OUTPUT NUMERICAL SUMMARY IF ANY end procedure update(line) # ... user inputted expressions appear here end procedure parse(text,delims) # ... converts string text to list w/respect to delims end USAGE AND DESCRIPTION OF THE IAWK/IGREP UTILITY USAGE : cont... USAGE: iawk infile [outfile] continued ... As you may have noticed, there is a goodly collection of global variables. These are very useful. The goal of the program is as follows Whatever update() returns, is what is displayed or written to the file. Below is a description of the global variables, in case you haven't figured it out from the source listing above. line - The line read in. num - The line number (position) within the input file. flags - These are used to toggle certain features. Currently only one flag "nocount" is supported. Append "nocount" to flags to suppress the final count summary. count - A table that holds counts of things. All non-zero entries are summarized at the end of the process. w1 is the width of the entry column (default=20). w2 is the width of the tally column (default=8). ebcdic - A mapping table for ascii/ebcdic file translations. unascii - A mapping table useful for wordstar print files. alpha - Alpha characters used with rot13. rot13 - Cheap and dirty encryption table. tty, crt, kbd - Used to force output to screen or take output from keyboard. These are all identical. You may begin the expressions section with such things as ... static var1,var2,... initial { var1 := .... var2 := ... } Remember that the object of the 'return' is what is displayed in an IGREP or written to the file in an IAWK. Upon completion several things happen. If the table count has been loaded with anything, a summary is output. This can be suppressed by appending "nocount" to the flags var- iable. Finally the grepawk files that were created to do the work are deleted unless "keep" is appended to the flags variable. The program itself follows. Clip and use (about 90 to 100 lines). #TITLE ICON EQUIVALENT OF GREP AND AWK #SUB ONE PROGRAM DOES BOTH #EJECT ################################################################# # # # IAWK.ICN 08/29/88 BY CHRIS TENAGLIA # # IGREP.ICN # # # ################################################################# procedure main(param) platform := "vms" # either unix, vms, or dos case platform of { "unix" : vdu := "/dev/tty" "vms" : vdu := "tt:" "dos" : vdu := "con" default : stop("platform ",platform," not recognized.") } front := [ "#################################################################", "# #", "# GREPAWK.ICN " || &dateline || " BY TENAGLIA #", "# #", "# THIS IS THE INTERMEDIATE PROGRAM THAT HANDLES THE MUNGING #", "# #", "#################################################################", "global count,crt,tty,alpha,rot13,ebcdic,kbd,lnum", "procedure main()", " alpha:= \042ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz\042", " rot13:= \042NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm\042", " ebcdic:= \042\\000\\001\\002\\003\\234\\011\\206\\177\\227\\215\\216\\013\\014\\015\\016\\017\042||", " \042\\020\\021\\022\\023\\235\\205\\010\\207\\030\\031\\222\\217\\034\\035\\036\\037\042||", " \042\\200\\201\\202\\203\\204\\012\\027\\033\\210\\211\\212\\213\\214\\005\\006\\007\042||", " \042\\220\\221\\026\\223\\224\\225\\226\\004\\230\\231\\232\\233\\024\\025\\236\\032\042||", " \042\\040\\240\\241\\242\\243\\244\\245\\246\\247\\250\\133\\056\\074\\050\\053\\041\042||", " \042\\046\\251\\252\\253\\254\\255\\256\\257\\260\\261\\135\\044\\052\\051\\073\\136\042||", " \042\\055\\057\\262\\263\\264\\265\\266\\267\\270\\271\\174\\054\\045\\137\\076\\077\042||", " \042\\272\\273\\274\\275\\276\\277\\300\\301\\302\\140\\072\\043\\100\\047\\075\\042\042||", " \042\\303\\141\\142\\143\\144\\145\\146\\147\\150\\151\\304\\305\\306\\307\\310\\311\042||", " \042\\312\\152\\153\\154\\155\\156\\157\\160\\161\\162\\313\\314\\315\\316\\317\\320\042||", " \042\\321\\176\\163\\164\\165\\166\\167\\170\\171\\172\\322\\323\\324\\325\\326\\327\042||", " \042\\330\\331\\332\\333\\334\\335\\336\\337\\340\\341\\342\\343\\344\\345\\346\\347\042||", " \042\\173\\101\\102\\103\\104\\105\\106\\107\\110\\111\\350\\351\\352\\353\\354\\355\042||", " \042\\175\\112\\113\\114\\115\\116\\117\\120\\121\\122\\356\\357\\360\\361\\362\\363\042||", " \042\\134\\237\\123\\124\\125\\126\\127\\130\\131\\132\\364\\365\\366\\367\\370\\371\042||", " \042\\060\\061\\062\\063\\064\\065\\066\\067\\070\\071\\372\\373\\374\\375\\376\\377\042", " crt := open(\042" || vdu || "\042,\042b\042)", " kbd := crt ; tty := crt", " lnum := 1", " count := table(0)", " while line := read() do", " {", " write(update(line,lnum))", " lnum +:= 1", " }", " write(crt,\042\\e[1;7mProcess Completed !\\e[m\042)", " end", "procedure parse(line,vals)", " static chars", " initial chars := &cset -- vals", " tokens := []", " line ? while tab(upto(chars)) do put(tokens,tab(many(chars)))", " return tokens", " end", "procedure update(line,num)" ] items := [] if *param = 0 then stop("Missing filespec(s)\n", "Usage: (awk or grep) infile [outfile]") if *param = 1 then write("Grepping ",param[1],"\n") if *param = 2 then write("Awking from ",param[1]," to ",param[2],"\n") repeat { writes("\e[1;7m_expression :\e[m ") if not(line := read()) then break if line == "" then break put(items,(" " || line)) } (out := open("grepawk.icn","w")) | stop("Can't create grepawk.icn !") every write(out,!front) every write(out,!items) write(out," end") close(out) write("\n\e[1;7mCompiling expressions for transmogrification.\e[m\n") From := (" <" || param[1])|"" To := (" >" || param[2])|"" system(("icont grepawk.icn -x" || From || To)) if not find("keep",flags) then { while remove("grepawk.icn") do writes(".") while remove("grepawk.icx") do writes(".") while remove("grepawk") do writes(".") } end #########################################################################