Hello World (AWK)

From LiteratePrograms

Jump to: navigation, search
Other implementations: Ada | ALGOL 68 | Alice ML | Amiga E | Applescript | AspectJ | Assembly Intel x86 Linux | Assembly Intel x86 NetBSD | AWK | bash | BASIC | Batch files | C | C, Cairo | C, Xlib | Candle | Clojure | C++ | C# | Delphi | Dylan | E | Eiffel | Erlang | Forth | FORTRAN | Fortress | Go | Groovy | Haskell | Hume | IBM PC bootstrap | Inform 7 | Java | Java, Swing | JavaScript | LaTeX | Lisp | Logo | Lua | Maple | MATLAB | Mercury | OCaml/F Sharp | occam | Oz | Pascal | Perl | PHP | Pic | PIR | PLI | PostScript | Prolog | Python | Rexx | Ruby | Scala | Scheme | Seed7 | sh | Smalltalk | SQL | Standard ML | SVG | Tcl | Tcl Tk | Visual Basic | Visual Basic .NET | XSL

This short AWK program prints a message on the standard output stream.

An AWK program consist of patterns and actions: each line starts with a pattern, followed by the action to execute when the pattern is found.

Patterns are regular expressions that are compared to each input line. If a match is found, the corresponding action is executed. There are two special patterns, BEGIN and END, that are matched respectively before the first line and after the last line. An empty pattern will match every line.
Actions are enclosed in curly braces. If the action is omitted, the matched lines are just printed.

In this simple program, we use the BEGIN pattern, and the corresponding command is executed on program startup. The print statement will write its arguments on the standard output stream. The first line is a comment in AWK, but tells the operating system how to run the program. The -f option, followed by a file name tells AWK to execute the script stored in that file. Without the -f option AWK expects a script on the command line.

<<hello_world.awk>>=
#!/usr/bin/awk -f
BEGIN {print "Hello, World!"}
Download code
Views
Personal tools