The GNU Awk User's Guide
Getting started with awk
awk: syntax error near line 1
awk: illegal statement near line 1
gawk: cmd. line:1: {print $0,}
gawk: cmd. line:1: ^ parse error
Effective AWK Programming
Gawk
Awk by example, Part 3
In this conclusion to the awk series, Daniel introduces you to awk's important string functions, and then shows you how to write a complete checkbook-balancing program from scratch. Along the way, you'll learn how to write your own functions and use awk's multidimensional arrays. By the end of this article, you'll have even more awk experience, allowing you to create more powerful scripts.
Formatting output
While awk's print statement does do the job most of the time, sometimes more is needed. For those times, awk offers two good old friends called printf() and sprintf(). Yes, these functions, like so many other awk parts, are identical to their C counterparts. printf() will print a formatted string to stdout, while sprintf() returns a formatted string that can be assigned to a variable. If you're not familiar with printf() and sprintf(), an introductory C text will quickly get you up to speed on these two essential printing functions. You can view the printf() man page by typing "man 3 printf" on your Linux system.
Awk by example, Part 2
Awk by example, Part 1
Sure, awk doesn't have a great name. But it is a great language. Awk is geared toward text processing and report generation, yet features many well-designed features that allow for serious programming. And, unlike some languages, awk's syntax is familiar, and borrows some of the best parts of languages like C, python, and bash (although, technically, awk was created before both python and bash). Awk is one of those languages that, once learned, will become a key part of your strategic coding arsenal.
An Awk Primer
The Awk text-processing programming language is a useful and simple tool for manipulating text. This document provides a quick tutorial for Awk. The Awk syntax used in this document corresponds to that used on UN*X. It may vary slightly on other platforms.
The Awk text-processing language is useful for such tasks as:
- Tallying information from text files and creating reports from the results.
- Adding additional functions to text editors like "vi".
- Translating files from one format to another.
- Creating small databases.
- Performing mathematical operations on files of numeric data.
Awk has two faces: it is a utility for performing simple text-processing tasks, and it is a programming language for performing complex text-processing tasks.
The two faces are really the same, however. Awk uses the same mechanisms for handling any text-processing task, but these mechanisms are flexible enough to allow useful Awk programs to be entered on the command line, or to implement complicated programs containing dozens of lines of Awk statements.
Awk statements comprise a programming language. In fact, Awk is useful for simple, quick-and-dirty computational programming. Anybody who can write a BASIC program can use Awk, although Awk's syntax is different from that of BASIC. Anybody who can write a C program can use Awk with little difficulty, and those who would like to learn C may find Awk a useful stepping stone, with the caution that Awk and C have significant differences beyond their many similarities.
There are, however, things that Awk is not. It is not really well suited for extremely large, complicated tasks. It is also an "interpreted" language -- that is, an Awk program cannot run on its own, it must be executed by the Awk utility itself. That means that it is relatively slow, though it is efficient as interpretive languages go, and that the program can only be used on systems that have Awk. There are translators available that can convert Awk programs into C code for compilation as stand-alone programs, but such translators have to be purchased separately.
One last item before proceeding: What does the name "Awk" mean? Awk actually stands for the names of its authors: "Aho, Weinberger, & Kernighan". Kernighan later noted: "Naming a language after its authors ... shows a certain poverty of imagination." The name is reminiscent of that of an oceanic bird known as an "auk", and so the picture of an auk often shows up on the cover of books on Awk.
Popular Posts
-
Compiled By J. Purcel, Red Hat Software Inc. Red Hat Linux Complete Command Reference book is divided into 9 Parts. They are User Commands S...
-
by Graham Williams Welcome to the world of GNU/Linux, liberating the computing desktop from the shackles of proprietary interests. The aim o...
-
This posting helps you to get free linux and unix ebooks and tutorials explaining unix commands, unix shell, unix programming, unix shell sc...
-
By Bill Ball Welcome to Linux! You hold in your hands everythin you need to install and use one of the most powerful computer operating sys...
-
By William Ball Welcome to Red Hat Linux! If you're new to Linux, choosing a Linux distribution to install can be confusing. Relax! You...
-
The University of Chicago If you are just starting with Unix, you should probably begin by reading the Unix: Getting Your Feet Wet document...
-
Introduction to Linux : A hands on Guide is written by Machtelt Garrels. This linux ebook covers linux history, properties of linux, linux a...
-
by Alan Dix UNIX Systems Programming I Content: File I/O, filters and file manipulation. Command line arguments and environment variables. ...
-
By Robin Burk and David B. Horvath Our highly popular first edition brought comprehensive, up-to-date information on UNIX to a wide audience...
-
By Tim Parker UNIX system administration used to be a skill learned by watching others, trying many things on spec, and scouring obscure mag...