Writing an AWK ScriptAt the bottom of this page, there is a list of references for learning AWK and places in the AWK internet community. Assuming that you have a Linux / Unix operating system with AWK on it, it will take a solid 8 hours of your time to read the stuff on these pages and get comfortable with AWK and get a few scripts through the edit-run-output steps and organize the input/script/output files in whatever folders you set up for them on your computer. (My experience is that if you rush this process, it will screw up, you'll get disgusted, decide it's not worth it, quit and go back to writing spreadsheets. The fastest path to the goal here is to take it slow at first.) Of course, you can spend all the time you want beyond that to read and become more expert, and refine your folders, etc. etc. At some point down the road you may decide to use a graphical interface to call in the awkscripts that are scattered about your folders. That is a separate topic . Writing AWK scripts requires only a basic text editor (not a word processor), like gedit, kedit, notepad, etc etc. Each command is simply enclosed in { } brackets, so both the computer and you can figure out what it is you want to do. Before you leave this page, please read the footnote. Here is a script that calculates the natural frequency (in cps) of a cantilever steel beam (2"X2"X36"), using US units: BEGIN { {E=29000000.} {Momin=(2*2^3)/12} {Area=2*2} {Dens=.283} {L=36} {freq=3.52*sqrt(E*386.04/Dens)*sqrt(Momin/Area)/(L^2)/6.2832} {print"frequency = " freq} } # END {} If you save this script as a text file (say beam.awk), cd to the folder it is stored in and at the command line type in awk -f beam.awk . The answer frequency = 49.6384 will appear in the console window. Instead of putting the numeric data in the script itself (as in the example above, which is not a good general practice), the input data may also be stored in a file like this (possibly along with lots of other data needed for other purposes): |
![]() Actually, the file need not be neatly formatted into fixed column widths as shown above. It could simply be spaced separated fields on each line like this: #Beams Beams Beams #Beams Beams Beams Comma-separated and space-separated file formats are often used when saving data from a spreadsheet to a simple text file. In any case, the AWK script must read the file to extract the needed variables. In the script below, the numeric value of each variable that the script needs to calculate frequency is assigned based on the first field ($1) on each line of the input file. The values in this example are set equal to the second field ($2) on the line. (In the input data file for this example, the fields are separated by spaces, so the Field Separator is defined in the BEGIN command as a space.) BEGIN {FS=" "} Notice now that the BEGIN sequence (which is delineated by the opening { and the closing } only includes the FS=" " command. The file reading commands have been moved out of the BEGIN sequence and also placed before the END sequence. (You need to read up on AWK to clearly understand this. For the moment just go with the flow here.) Commands in the file reading sequence like {if($1=="Length") {L=$2}} simply say "If the first field of the input line is Length, set the value of AWK variable L equal to whatever is in the second field on that line." If you think about this simple, powerful syntax, it is a most elegant way of reading information and acting on it. You can read the most awfully formatted input files and extract data easily. In fact, this is the need that initially drove me to investigate AWK (and I never looked back again). Hence my comment that AWK is "supremely elegant". And finally, commands in the END sequence do the actual calculations and printout. If the data in the input file is separated by commas and not spaces, the BEGIN command would be If the AWK script shown above is stored in a text file named [beam.awk] and the input data file is in [indata.txt]
from the command line prompt, you would execute this script and write the output to a file named [beam.out] using the following command: Sample script files will be posted on the awkscripts page as time goes by. They will run from the command prompt without the need for a graphical interface. FootnoteAWK is a much more powerful Linux / Unix utility than we are using it for in writing these engineering scripts. Its true strength lies in its concise, inherent ability to easily process information and data in an alpha-numeric text file (e.g., a huge, maybe badly formatted, output file from some engineering code). That strength is not being put to use in the work we are doing here. Actually, I became an AWK-addict when I discovered it's power to process a horrendous output file I had to deal with many times a day.... AWK literally saved my technical butt, allowing me to run through easily 10X the post-processing cases (I am tempted to say 100X ! )that I could normally run. As time went on I saw the use of AWK in writing these neat scripts, at the same time we were having problems transferring spreadsheets across people and systems, and the idea of AWK as an engineering scripting language for small problems fell into place. Most likely, Misters A., W., and K. didn't have this use in mind when they wrote AWK, but I am pleased to sing its praises as an "accidental" engineering tool. (dleo@net1plus.com) |
Reference and Resources for AWK
Reference and Resources for TCL- TK
|