Awk Programming
Awk Programming
Awk Programming
Awk is a scripting language used for manipulating data and generating reports. The awk
command programming language requires no compiling and allows the user to use
variables, numeric functions, string functions, and logical operators.
Awk is a utility that enables a programmer to write tiny but effective programs in the
form of statements that define text patterns that are to be searched for in each line of
a document and the action that is to be
Basic Structure
The essential organization of an AWK program follows the
form:
pattern { action }
adds one line before and one line after the input file. This
isn't very useful, but with a simple change, we can make this
into a typical AWK program:
I'll improve the script in the next sections, but we'll call it
"FileOwner". But let's not put it into a script or file yet. I will
cover that part in a bit. Hang on and follow with me so you
get the flavor of AWK.
The characters "\t" Indicates a tab character so the output
lines up on even boundries. The "$8" and "$3" have a
meaning similar to a shell script. Instead of the eighth and
third argument, they mean the eighth and third field of the
input line. You can think of a field as a column, and the
action you specify operates on each line or row read in.
{print "$8\t$3" }
That example would print "$8 $3". Inside the quotes, the
dollar sign is not a special character. Outside, it corresponds
to a field. What do I mean by the third and eight field?
Consider the Solaris "/usr/bin/ls -l" command, which has
eight columns of information. The System V version (Similar
to the Linux version), "/usr/5bin/ls -l" has 9 columns. The
third column is the owner, and the eighth (or nineth) column
in the name of the file. This AWK program can be used to
process the output of the "ls -l" command, printing out the
filename, then the owner, for each file. I'll show you how.
Types of AWK
Following are the variants of AWK −
AWK − Original AWK from AT & T Laboratory.
NAWK − Newer and improved version of AWK from AT & T Laboratory.
GAWK − It is GNU AWK. All GNU/Linux distributions ship GAWK. It is fully
compatible with AWK and NAWK.
Text processing,
Producing formatted text reports,
Performing arithmetic operations,
Performing string operations, and many more.