General Introduction ¶
This file documents awk, a program that you can use to select
particular records in a file and perform operations upon them.
Copyright © 1989, 1991, 1992, 1993, 1996–2005, 2007, 2009–2026
Free Software Foundation, Inc.
This is Edition 5.4 of GAWK: Effective AWK Programming: A User’s Guide for GNU Awk, for the 5.4.0 (or later) version of the GNU implementation of AWK.
Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.3 or any later version published by the Free Software Foundation; with the Invariant Sections being “GNU General Public License”, with the Front-Cover Texts being “A GNU Manual”, and with the Back-Cover Texts as in (a) below. A copy of the license is included in the section entitled “GNU Free Documentation License”.
- The FSF’s Back-Cover Text is: “You have the freedom to copy and modify this GNU manual.”
Short Table of Contents
Table of Contents
- Part I:
TheawkLanguage- 1 Getting Started with
awk - 2 Running
awkandgawk- 2.1 Invoking
awk - 2.2 Command-Line Options
- 2.3 Other Command-Line Arguments
- 2.4 Naming Standard Input
- 2.5 The Environment Variables
gawkUses - 2.6
gawk’s Exit Status - 2.7 Including Other Files into Your Program
- 2.8 Loading Dynamic Extensions into Your Program
- 2.9 Obsolete Options and/or Features
- 2.10 Undocumented Options and Features
- 2.11 Summary
- 2.1 Invoking
- 3 Regular Expressions
- 4 Reading Input Files
- 4.1 How Input Is Split into Records
- 4.2 Examining Fields
- 4.3 Nonconstant Field Numbers
- 4.4 Changing the Contents of a Field
- 4.5 Specifying How Fields Are Separated
- 4.6 Reading Fixed-Width Data
- 4.7 Defining Fields by Content
- 4.8 Checking How
gawkIs Splitting Records - 4.9 Multiple-Line Records
- 4.10 Explicit Input with
getline- 4.10.1 Using
getlinewith No Arguments - 4.10.2 Using
getlineinto a Variable - 4.10.3 Using
getlinefrom a File - 4.10.4 Using
getlineinto a Variable from a File - 4.10.5 Using
getlinefrom a Pipe - 4.10.6 Using
getlineinto a Variable from a Pipe - 4.10.7 Using
getlinefrom a Coprocess - 4.10.8 Using
getlineinto a Variable from a Coprocess - 4.10.9 Points to Remember About
getline - 4.10.10 Summary of
getlineVariants
- 4.10.1 Using
- 4.11 Reading Input with a Timeout
- 4.12 Retrying Reads After Certain Input Errors
- 4.13 Directories on the Command Line
- 4.14 Summary
- 4.15 Exercises
- 5 Printing Output
- 5.1 The
printStatement - 5.2
printStatement Examples - 5.3 Output Separators
- 5.4 Controlling Numeric Output with
print - 5.5 Using
printfStatements for Fancier Printing - 5.6 Redirecting Output of
printandprintf - 5.7 Special Files for Standard Preopened Data Streams
- 5.8 Special File names in
gawk - 5.9 Closing Input and Output Redirections
- 5.10 Speeding Up Pipe Output
- 5.11 Enabling Nonfatal Output
- 5.12 Summary
- 5.13 Exercises
- 5.1 The
- 6 Expressions
- 6.1 Constants, Variables, and Conversions
- 6.2 Operators: Doing Something with Values
- 6.3 Truth Values and Conditions
- 6.4 Function Calls
- 6.5 Operator Precedence (How Operators Nest)
- 6.6 Where You Are Makes a Difference
- 6.7 Summary
- 7 Patterns, Actions, and Variables
- 7.1 Pattern Elements
- 7.2 Using Shell Variables in Programs
- 7.3 Actions
- 7.4 Control Statements in Actions
- 7.5 Predefined Variables
- 7.6 Summary
- 8 Arrays in
awk - 9 Functions
- 9.1 Built-in Functions
- 9.2 User-Defined Functions
- 9.3 Variable Typing Is Dynamic
- 9.4 A Note On Shadowed Variables
- 9.5 Indirect Function Calls
- 9.6 Summary
- 1 Getting Started with
- Part II:
Problem Solving withawk- 10 A Library of
awkFunctions- 10.1 Naming Library Function Global Variables
- 10.2 General Programming
- 10.2.1 Converting Strings to Numbers
- 10.2.2 Assertions
- 10.2.3 Rounding Numbers
- 10.2.4 The Cliff Random Number Generator
- 10.2.5 Translating Between Characters and Numbers
- 10.2.6 Merging an Array into a String
- 10.2.7 Managing the Time of Day
- 10.2.8 Reading a Whole File at Once
- 10.2.9 Quoting Strings to Pass to the Shell
- 10.2.10 Checking Whether A Value Is Numeric
- 10.2.11 Producing CSV Data
- 10.3 Data file Management
- 10.4 Processing Command-Line Options
- 10.5 Reading the User Database
- 10.6 Reading the Group Database
- 10.7 Traversing Arrays of Arrays
- 10.8 Summary
- 10.9 Exercises
- 11 Practical
awkPrograms- 11.1 Running the Example Programs
- 11.2 Reinventing Wheels for Fun and Profit
- 11.3 A Grab Bag of
awkPrograms- 11.3.1 Finding Duplicated Words in a Document
- 11.3.2 An Alarm Clock Program
- 11.3.3 Transliterating Characters
- 11.3.4 Printing Mailing Labels
- 11.3.5 Generating Word-Usage Counts
- 11.3.6 Removing Duplicates from Unsorted Text
- 11.3.7 Extracting Programs from Texinfo Source Files
- 11.3.8 A Simple Stream Editor
- 11.3.9 An Easy Way to Use Library Functions
- 11.3.10 Finding Anagrams from a Dictionary
- 11.3.11 And Now for Something Completely Different
- 11.3.12 Demonstrating Shortest and Longest Match Operators
- 11.4 Summary
- 11.5 Exercises
- 10 A Library of
- Part III:
Moving Beyond Standardawkwithgawk- 12 Advanced Features of
gawk- 12.1 Allowing Nondecimal Input Data
- 12.2 Boolean Typed Values
- 12.3 Controlling Array Traversal and Array Sorting
- 12.4 Two-Way Communications with Another Process
- 12.5 Using
gawkfor Network Programming - 12.6 Profiling Your
awkPrograms - 12.7 Preserving Data Between Runs
- 12.8 Builtin Features versus Extensions
- 12.9 Summary
- 13 Internationalization with
gawk - 14 Debugging
awkPrograms - 15 Namespaces in
gawk- 15.1 Standard
awk’s Single Namespace - 15.2 Qualified Names
- 15.3 The Default Namespace
- 15.4 Changing The Namespace
- 15.5 Namespace and Component Naming Rules
- 15.6 Internal Name Management
- 15.7 Namespace Example
- 15.8 Including A File Without Changing The Namespace
- 15.9 Namespaces and Other
gawkFeatures - 15.10 Summary
- 15.1 Standard
- 16 Arithmetic and Arbitrary-Precision Arithmetic with
gawk- 16.1 A General Description of Computer Arithmetic
- 16.2 Other Stuff to Know
- 16.3 Arbitrary-Precision Arithmetic Features in
gawk - 16.4 Floating-Point Arithmetic: Caveat Emptor!
- 16.5 Arbitrary-Precision Integer Arithmetic with
gawk - 16.6 How To Check If MPFR Is Available
- 16.7 Standards Versus Existing Practice
- 16.8 Summary
- 17 Writing Extensions for
gawk- 17.1 Introduction
- 17.2 Extension Licensing
- 17.3 How It Works at a High Level
- 17.4 API Description
- 17.4.1 Introduction
- 17.4.2 General-Purpose Data Types
- 17.4.3 Memory Allocation Functions and Convenience Macros
- 17.4.4 Constructor Functions
- 17.4.5 Managing MPFR and GMP Values
- 17.4.6 Registration Functions
- 17.4.7 Printing Messages
- 17.4.8 Updating
ERRNO - 17.4.9 Requesting Values
- 17.4.10 Accessing and Updating Parameters
- 17.4.11 Symbol Table Access
- 17.4.12 Array Manipulation
- 17.4.13 Accessing and Manipulating Redirections
- 17.4.14 API Variables
- 17.4.15 Boilerplate Code
- 17.4.16 Changes From Version 1 of the API
- 17.5 How
gawkFinds Extensions - 17.6 Example: Some File Functions
- 17.7 The Sample Extensions in the
gawkDistribution- 17.7.1 File-Related Functions
- 17.7.2 Interface to
fnmatch() - 17.7.3 Interface to
fork(),wait(), andwaitpid() - 17.7.4 Enabling In-Place File Editing
- 17.7.5 Character and Numeric values:
ord()andchr() - 17.7.6 Reading Directories
- 17.7.7 Reversing Output
- 17.7.8 Two-Way I/O Example
- 17.7.9 Dumping and Restoring an Array
- 17.7.10 Reading an Entire File
- 17.7.11 Extension Time Functions
- 17.7.12 API Tests
- 17.8 The
gawkextlibProject - 17.9 Summary
- 17.10 Exercises
- 12 Advanced Features of
- Part IV:
Appendices- Appendix A The Evolution of the
awkLanguage- A.1 Major Changes Between V7 and SVR3.1
- A.2 Changes Between SVR3.1 and SVR4
- A.3 Changes Between SVR4 and POSIX
awk - A.4 Extensions in Brian Kernighan’s
awk - A.5 Extensions in
gawkNot in POSIXawk - A.6 History of
gawkFeatures - A.7 Common Extensions Summary
- A.8 Regexp Ranges and Locales: A Long Sad Story
- A.9 Major Contributors to
gawk - A.10 Summary
- Appendix B Installing
gawk - Appendix C Implementation Notes
- Appendix D Basic Programming Concepts
- Glossary
- GNU General Public License
- GNU Free Documentation License
- Index
- Appendix A The Evolution of the
Next: Foreword to the Fourth Edition, Previous: General Introduction, Up: General Introduction [Contents][Index]
Foreword to the Third Edition ¶
Arnold Robbins and I are good friends. We were introduced
in 1990
by circumstances—and our favorite programming language, AWK.
The circumstances started a couple of years
earlier. I was working at a new job and noticed an unplugged
Unix computer sitting in the corner. No one knew how to use it,
and neither did I. However,
a couple of days later, it was running, and
I was root and the one-and-only user.
That day, I began the transition from statistician to Unix programmer.
On one of many trips to the library or bookstore in search of
books on Unix, I found the gray AWK book, a.k.a.
Alfred V. Aho, Brian W. Kernighan, and
Peter J. Weinberger’s The AWK Programming Language (Addison-Wesley,
1988). awk’s simple programming paradigm—find a pattern in the
input and then perform an action—often reduced complex or tedious
data manipulations to a few lines of code. I was excited to try my
hand at programming in AWK.
Alas, the awk on my computer was a limited version of the
language described in the gray book. I discovered that my computer
had “old awk” and the book described
“new awk.”
I learned that this was typical; the old version refused to step
aside or relinquish its name. If a system had a new awk, it was
invariably called nawk, and few systems had it.
The best way to get a new awk was to ftp the source code for
gawk from prep.ai.mit.edu. gawk was a version of
new awk written by David Trueman and Arnold, and available under
the GNU General Public License.
(Incidentally,
it’s no longer difficult to find a new awk. gawk ships with
GNU/Linux, and you can download binaries or source code for almost
any system; my wife uses gawk on her VMS box.)
My Unix system started out unplugged from the wall; it certainly was not
plugged into a network. So, oblivious to the existence of gawk
and the Unix community in general, and desiring a new awk, I wrote
my own, called mawk.
Before I was finished, I knew about gawk,
but it was too late to stop, so I eventually posted
to a comp.sources newsgroup.
A few days after my posting, I got a friendly email
from Arnold introducing
himself. He suggested we share design and algorithms and
attached a draft of the POSIX standard so
that I could update mawk to support language extensions added
after publication of The AWK Programming Language.
Frankly, if our roles had been reversed, I would not have been so open and we probably would have never met. I’m glad we did meet. He is an AWK expert’s AWK expert and a genuinely nice person. Arnold contributes significant amounts of his expertise and time to the Free Software Foundation.
This book is the gawk reference manual, but at its core it
is a book about AWK programming that
will appeal to a wide audience.
It is a definitive reference to the AWK language as defined by the
1987 Bell Laboratories release and codified in the 1992 POSIX Utilities
standard.
On the other hand, the novice AWK programmer can study
a wealth of practical programs that emphasize
the power of AWK’s basic idioms:
data-driven control flow, pattern matching with regular expressions,
and associative arrays.
Those looking for something new can try out gawk’s
interface to network protocols via special /inet files.
The programs in this book make clear that an AWK program is typically much smaller and faster to develop than a counterpart written in C. Consequently, there is often a payoff to prototyping an algorithm or design in AWK to get it running quickly and expose problems early. Often, the interpreted performance is adequate and the AWK prototype becomes the product.
The new pgawk (profiling gawk), produces
program execution counts.
I recently experimented with an algorithm that for
n
lines of input, exhibited
~ C n^2
performance, while
theory predicted
~ C n log n
behavior. A few minutes poring
over the awkprof.out profile pinpointed the problem to
a single line of code. pgawk is a welcome addition to
my programmer’s toolbox.
Arnold has distilled over a decade of experience writing and
using AWK programs, and developing gawk, into this book. If you use
AWK or want to learn how, then read this book.
Michael Brennan
Author of mawk
March 2001
Next: Preface, Previous: Foreword to the Third Edition, Up: General Introduction [Contents][Index]
Foreword to the Fourth Edition ¶
Some things don’t change. Thirteen years ago I wrote: “If you use AWK or want to learn how, then read this book.” True then, and still true today.
Learning to use a programming language is about more than mastering the syntax. One needs to acquire an understanding of how to use the features of the language to solve practical programming problems. A focus of this book is many examples that show how to use AWK.
Some things do change. Our computers are much faster and have more memory. Consequently, speed and storage inefficiencies of a high-level language matter less. Prototyping in AWK and then rewriting in C for performance reasons happens less, because more often the prototype is fast enough.
Of course, there are computing operations that are best done in C or C++.
With gawk 4.1 and later, you do not have to choose between writing
your program in AWK or in C/C++. You can write most of your
program in AWK and the aspects that require C/C++ capabilities can be written
in C/C++, and then the pieces glued together when the gawk module loads
the C/C++ module as a dynamic plug-in.
Writing Extensions for gawk,
has all the
details, and, as expected, many examples to help you learn the ins and outs.
I enjoy programming in AWK and had fun (re)reading this book. I think you will too.
Michael Brennan
Author of mawk
October 2014
Next: Getting Started with awk, Previous: Foreword to the Fourth Edition, Up: General Introduction [Contents][Index]
Preface ¶
Several kinds of tasks occur repeatedly when working with text files.
You might want to extract certain lines and discard the rest. Or you
may need to make changes wherever certain patterns appear, but leave the
rest of the file alone. Such jobs are often easy with awk.
The awk utility interprets a special-purpose programming
language that makes it easy to handle simple data-reformatting jobs.
The GNU implementation of awk is called gawk; if you
invoke it with the proper options or environment variables,
it is fully compatible with
the POSIX1
specification of the awk language
and with the Unix version of awk maintained
by Brian Kernighan.
This means that all
properly written awk programs should work with gawk.
So most of the time, we don’t distinguish between gawk and other
awk implementations.
Using awk you can:
- Manage small, personal databases
- Generate reports
- Validate data
- Produce indexes and perform other document-preparation tasks
- Experiment with algorithms that you can adapt later to other computer languages
In addition,
gawk
provides facilities that make it easy to:
- Extract bits and pieces of data for processing
- Sort data
- Perform simple network communications
- Profile and debug
awkprograms - Extend the language with functions written in C or C++
This Web page teaches you about the awk language and
how you can use it effectively. You should already be familiar with basic
system commands, such as cat and ls,2 as well as basic shell
facilities, such as input/output (I/O) redirection and pipes.
Implementations of the awk language are available for many
different computing environments. This Web page, while describing
the awk language in general, also describes the particular
implementation of awk called gawk (which stands for
“GNU awk”). gawk runs on a broad range of Unix systems,
ranging from Intel-architecture PC-based computers
up through large-scale systems.
gawk has also been ported to macOS, z/OS,
Microsoft Windows
(all versions),
and OpenVMS.3
- History of
awkandgawk - A Rose by Any Other Name
- Using This Book
- Typographical Conventions
- The GNU Project and This Book
- How to Contribute
- Acknowledgments
Next: A Rose by Any Other Name, Up: Preface [Contents][Index]
History of awk and gawk ¶
| Recipe for a Programming Language | ||||||
|---|---|---|---|---|---|---|
Blend all parts well using After eight years, add another part After 35 more years, add Unicode and CSV support, sprinkle lightly with
a few choice features from |
The name awk comes from the initials of its designers: Alfred V.
Aho, Peter J. Weinberger, and Brian W. Kernighan. The original version of
awk was written in 1977 at AT&T Bell Laboratories.
In 1985, a new version made the programming
language more powerful, introducing user-defined functions, multiple input
streams, and computed regular expressions.
This new version became widely available with Unix System V
Release 3.1 (1987).
The version in System V Release 4 (1989) added some new features and cleaned
up the behavior in some of the “dark corners” of the language.
The specification for awk in the POSIX Command Language
and Utilities standard further clarified the language.
Both the gawk designers and the original awk designers at Bell Laboratories
provided feedback for the POSIX specification.
Paul Rubin wrote gawk in 1986.
Jay Fenlason completed it, with advice from Richard Stallman. John Woods
contributed parts of the code as well. In 1988 and 1989, David Trueman, with
help from me, thoroughly reworked gawk for compatibility
with the newer awk.
Circa 1994, I became the primary maintainer.
Current development focuses on bug fixes,
performance improvements, standards compliance, and, occasionally, new features.
In May 1997, Jürgen Kahrs felt the need for network access
from awk, and with a little help from me, set about adding
features to do this for gawk. At that time, he also
wrote the bulk of
TCP/IP Internetworking with gawk
(a separate document, available as part of the gawk distribution).
His code finally became part of the main gawk distribution
with gawk version 3.1.
John Haque rewrote the gawk internals, in the process providing
an awk-level debugger. This version became available as
gawk version 4.0 in 2011.
See Major Contributors to gawk
for a full list of those who have made important contributions to gawk.
Next: Using This Book, Previous: History of awk and gawk, Up: Preface [Contents][Index]
A Rose by Any Other Name ¶
The awk language has evolved over the years. Full details are
provided in The Evolution of the awk Language.
The language described in this Web page
is often referred to as “new awk.”
By analogy, the original version of awk is
referred to as “old awk.”
On most current systems, when you run the awk utility
you get some version of new awk.4 If your system’s standard
awk is the old one, you will see something like this
if you try the following test program:
$ awk 1 /dev/null error→ awk: syntax error near line 1 error→ awk: bailing out near line 1
In this case, you should find a version of new awk,
or just install gawk!
Throughout this Web page, whenever we refer to a language feature
that should be available in any complete implementation of POSIX awk,
we simply use the term awk. When referring to a feature that is
specific to the GNU implementation, we use the term gawk.
Next: Typographical Conventions, Previous: A Rose by Any Other Name, Up: Preface [Contents][Index]
Using This Book ¶
The term awk refers to a particular program as well as to the language you
use to tell this program what to do. When we need to be careful, we call
the language “the awk language,”
and the program “the awk utility.”
This Web page explains
both how to write programs in the awk language and how to
run the awk utility.
The term “awk program” refers to a program written by you in
the awk programming language.
Primarily, this Web page explains the features of awk
as defined in the POSIX standard. It does so in the context of the
gawk implementation. While doing so, it also
attempts to describe important differences between gawk
and other awk
implementations.5
Finally, it notes any gawk features that are not in
the POSIX standard for awk.
This Web page has the difficult task of being both a tutorial and a reference. If you are a novice, feel free to skip over details that seem too complex. You should also ignore the many cross-references; they are for the expert user and for the Info and HTML versions of the Web page.
There are sidebars scattered throughout the Web page. They add a more complete explanation of points that are relevant, but not likely to be of interest on first reading. All appear in the index, under the heading “sidebar.”
Most of the time, the examples use complete awk programs.
Some of the more advanced sections show only the part of the awk
program that illustrates the concept being described.
Although this Web page is aimed principally at people who have not been
exposed
to awk, there is a lot of information here that even the awk
expert should find useful. In particular, the description of POSIX
awk and the example programs in
A Library of awk Functions, and
in
Practical awk Programs,
should be of interest.
This Web page is split into several parts, as follows:
- Part I describes the
awklanguage and thegawkprogram in detail. It starts with the basics, and continues through all of the features ofawk. It contains the following chapters:- Getting Started with
awk, provides the essentials you need to know to begin usingawk. - Running
awkandgawk, describes how to rungawk, the meaning of its command-line options, and how it findsawkprogram source files. - Regular Expressions,
introduces regular expressions in general, and in particular the flavors
supported by POSIX
awkandgawk. - Reading Input Files,
describes how
awkreads your data. It introduces the concepts of records and fields, as well as thegetlinefunction. I/O redirection is first described here. Network I/O is also briefly introduced here. - Printing Output,
describes how
awkprograms can produce output withprintandprintf. - Expressions, describes expressions, which are the basic building blocks for getting most things done in a program.
- Patterns, Actions, and Variables,
describes how to write patterns for matching records, actions for
doing something when a record is matched, and the predefined variables
awkandgawkuse. - Arrays in
awk, coversawk’s one-and-only data structure: the associative array. Deleting array elements and whole arrays is described, as well as sorting arrays ingawk. The chapter also describes howgawkprovides arrays of arrays. - Functions,
describes the built-in functions
awkandgawkprovide, as well as how to define your own functions. It also discusses howgawklets you call functions indirectly.
- Getting Started with
- Part II shows how to use
awkandgawkfor problem solving. There is lots of code here for you to read and learn from. This part contains the following chapters:- A Library of
awkFunctions, provides a number of functions meant to be used from mainawkprograms. - Practical
awkPrograms, provides many sampleawkprograms.
Reading these two chapters allows you to see
awksolving real problems. - A Library of
- Part III focuses on features specific to
gawk. It contains the following chapters:- Advanced Features of
gawk, describes a number of advanced features. Of particular note are the abilities to control the order of array traversal, have two-way communications with another process, perform TCP/IP networking, and profile yourawkprograms. - Internationalization with
gawk, describes special features for translating program messages into different languages at runtime. - Debugging
awkPrograms, describes thegawkdebugger. - Namespaces in
gawk, describes howgawkallows variables and/or functions of the same name to be in different namespaces. - Arithmetic and Arbitrary-Precision Arithmetic with
gawk, describes advanced arithmetic facilities. - Writing Extensions for
gawk, describes how to add new variables and functions togawkby writing extensions in C or C++.
- Advanced Features of
- Part IV provides the appendices, the Glossary, and two licenses that cover
the
gawksource code and this Web page, respectively. It contains the following appendices:- The Evolution of the
awkLanguage, describes how theawklanguage has evolved since its first release to the present. It also describes howgawkhas acquired features over time. - Installing
gawk, describes how to getgawk, how to compile it on POSIX-compatible systems, and how to compile and use it on different non-POSIX systems. It also describes how to report bugs ingawkand where to get other freely availableawkimplementations. - Implementation Notes,
describes how to disable
gawk’s extensions, as well as how to contribute new code togawk, and some possible future directions forgawkdevelopment. - Basic Programming Concepts, provides some very cursory background material for those who are completely unfamiliar with computer programming.
- The Glossary, defines most, if not all, of the significant terms used throughout the Web page. If you find terms that you aren’t familiar with, try looking them up here.
- GNU General Public License, and
GNU Free Documentation License,
present the licenses that cover the
gawksource code and this Web page, respectively.
- The Evolution of the
Next: The GNU Project and This Book, Previous: Using This Book, Up: Preface [Contents][Index]
Typographical Conventions ¶
This Web page is written in Texinfo, the GNU documentation formatting language. A single Texinfo source file is used to produce both the printed and online versions of the documentation. Because of this, the typographical conventions are slightly different than in other books you may have read.
Examples you would type at the command line are preceded by the common shell primary and secondary prompts, ‘$’ and ‘>’, respectively. Input that you type is shown like this. Output from the command is preceded by the glyph “-|”. This typically represents the command’s standard output. Error messages and other output on the command’s standard error are preceded by the glyph “error→”. For example:
$ echo hi on stdout -| hi on stdout $ echo hello on stderr 1>&2 error→ hello on stderr
In the text, almost anything related to programming, such as
command names,
variable and function names, and string, numeric and regexp constants
appear in this font. Code fragments
appear in the same font and quoted, ‘like this’.
Things that are replaced by the user or programmer
appear in this font.
Options look like this: -f.
File names are indicated like this: /path/to/ourfile.
Some things are
emphasized like this, and if a point needs to be made
strongly, it is done like this.
The first occurrence of
a new term is usually its definition and appears in the same
font as the previous occurrence of “definition” in this sentence.
Characters that you type at the keyboard look like this. In particular, there are special characters called “control characters.” These are characters that you type by holding down both the CONTROL key and another key, at the same time. For example, a Ctrl-d is typed by first pressing and holding the CONTROL key, next pressing the d key, and finally releasing both keys.
For the sake of brevity, throughout this Web page, we refer to
Brian Kernighan’s version of awk as “BWK awk.”
(See Other Freely Available awk Implementations for information on his and other versions.)
Dark Corners ¶
Dark corners are basically fractal—no matter how much you illuminate, there’s always a smaller but darker one.
— Brian Kernighan
Until the POSIX standard (and GAWK: Effective AWK Programming),
many features of awk were either poorly documented or not
documented at all. Descriptions of such features
(often called “dark corners”) are noted in this Web page with
“(d.c.).”
They also appear in the index under the heading “dark corner.”
But, as noted by the opening quote, any coverage of dark corners is by definition incomplete.
Extensions to the standard awk language that are supported by
more than one awk implementation are marked
“(c.e.),” and listed in the index under “common extensions”
and “extensions, common.”
Next: How to Contribute, Previous: Typographical Conventions, Up: Preface [Contents][Index]
The GNU Project and This Book ¶
The Free Software Foundation (FSF) is a nonprofit organization dedicated to the production and distribution of freely distributable software. It was founded by Richard M. Stallman, the author of the original Emacs editor. GNU Emacs is the most widely used version of Emacs today.
The GNU6
Project is an ongoing effort on the part of the Free Software
Foundation to create a complete, freely distributable, POSIX-compliant
computing environment.
The FSF uses the GNU General Public License (GPL) to ensure that
its software’s
source code is always available to the end user.
A copy of the GPL is included
in this Web page
for your reference
(see GNU General Public License).
The GPL applies to the C language source code for gawk.
To find out more about the FSF and the GNU Project online,
see the GNU Project’s home page.
This Web page may also be read from
GNU’s website.
A shell, an editor (Emacs), highly portable optimizing C, C++, and
Objective-C compilers, a symbolic debugger and dozens of large and
small utilities (such as gawk), have all been completed and are
freely available. The GNU operating
system kernel (the HURD), has been released but remains in an early
stage of development.
Until the GNU operating system is more fully developed, you should consider using GNU/Linux, a freely distributable, Unix-like operating system for Intel, Power Architecture, Sun SPARC, IBM S/390, and other systems.7 Many GNU/Linux distributions are available for download from the Internet.
The Web page you are reading is actually free—at least, the
information in it is free to anyone. The machine-readable
source code for the Web page comes with gawk.
(Take a moment to check the Free Documentation
License in GNU Free Documentation License.)
The Web page itself has gone through multiple previous editions.
Paul Rubin wrote the very first draft of The GAWK Manual;
it was around 40 pages long.
Diane Close and Richard Stallman improved it, yielding a
version that was
around 90 pages and barely described the original, “old”
version of awk.
I started working with that version in the fall of 1988.
As work on it progressed,
the FSF published several preliminary versions (numbered 0.x).
In 1996, edition 1.0 was released with gawk 3.0.0.
The FSF published the first two editions under
the title The GNU Awk User’s Guide.
This edition maintains the basic structure of the previous editions.
For FSF edition 4.0, the content was thoroughly reviewed and updated. All
references to gawk versions prior to 4.0 were removed.
Of significant note for that edition was the addition of Debugging awk Programs.
For FSF edition
5.0,
the content has been reorganized into parts,
and the major new additions are Arithmetic and Arbitrary-Precision Arithmetic with gawk,
and Writing Extensions for gawk.
This Web page will undoubtedly continue to evolve. If you find an error in the Web page, please report it! See Reporting Problems and Bugs for information on submitting problem reports electronically.
Next: Acknowledgments, Previous: The GNU Project and This Book, Up: Preface [Contents][Index]
How to Contribute ¶
As the maintainer of GNU awk, I once thought that I would be
able to manage a collection of publicly available awk programs
and I even solicited contributions. Making things available on the Internet
helps keep the gawk distribution down to manageable size.
The initial collection of material, such as it is, is still available at ftp://ftp.freefriends.org/arnold/Awkstuff.
In the hopes of doing something broader, I acquired the
awklang.org domain. Late in 2017, a volunteer took on the task
of managing it.
If you have written an interesting awk program that
you would like to share with the rest of the world, please see
http://www.awklang.org and use the “Contact” link.
If you have written a gawk extension, please see
The gawkextlib Project.
Previous: How to Contribute, Up: Preface [Contents][Index]
Acknowledgments ¶
The initial draft of The GAWK Manual had the following acknowledgments:
Many people need to be thanked for their assistance in producing this manual. Jay Fenlason contributed many ideas and sample programs. Richard Mlynarik and Robert Chassell gave helpful comments on drafts of this manual. The paper A Supplemental Document for AWK by John W. Pierce of the Chemistry Department at UC San Diego, pinpointed several issues relevant both to
awkimplementation and to this manual, that would otherwise have escaped us.
I would like to acknowledge Richard M. Stallman, for his vision of a better world and for his courage in founding the FSF and starting the GNU Project.
Earlier editions of this Web page had the following acknowledgements:
The following people (in alphabetical order) provided helpful comments on various versions of this book: Rick Adams, Dr. Nelson H.F. Beebe, Karl Berry, Dr. Michael Brennan, Rich Burridge, Claire Cloutier, Diane Close, Scott Deifik, Christopher (“Topher”) Eliot, Jeffrey Friedl, Dr. Darrel Hankerson, Michal Jaegermann, Dr. Richard J. LeBlanc, Michael Lijewski, Pat Rankin, Miriam Robbins, Mary Sheehan, and Chuck Toporek.
Robert J. Chassell provided much valuable advice on the use of Texinfo. He also deserves special thanks for convincing me not to title this Web page How to Gawk Politely. Karl Berry helped significantly with the TeX part of Texinfo.
I would like to thank Marshall and Elaine Hartholz of Seattle and Dr. Bert and Rita Schreiber of Detroit for large amounts of quiet vacation time in their homes, which allowed me to make significant progress on this Web page and on
gawkitself.Phil Hughes of SSC contributed in a very important way by loaning me his laptop GNU/Linux system, not once, but twice, which allowed me to do a lot of work while away from home.
David Trueman deserves special credit; he has done a yeoman job of evolving
gawkso that it performs well and without bugs. Although he is no longer involved withgawk, working with him on this project was a significant pleasure.The intrepid members of the GNITS mailing list, and most notably Ulrich Drepper, provided invaluable help and feedback for the design of the internationalization features.
Chuck Toporek, Mary Sheehan, and Claire Cloutier of O’Reilly & Associates contributed significant editorial help for this Web page for the 3.1 release of
gawk.
Dr. Nelson Beebe,
Andreas Buening,
Dr. Manuel Collado,
Antonio Colombo,
Stephen Davies,
Scott Deifik,
Akim Demaille,
Daniel Richard G.,
Juan Manuel Guerrero,
Darrel Hankerson,
Michal Jaegermann,
Jürgen Kahrs,
Stepan Kasal,
John Malmberg,
Chet Ramey,
Pat Rankin,
Andrew Schorr,
Corinna Vinschen,
and Eli Zaretskii
(in alphabetical order)
make up the current gawk “crack portability team.” Without
their hard work and help, gawk would not be nearly the robust,
portable program it is today. It has been and continues to be a pleasure
working with this team of fine people.
Notable code and documentation contributions were made by
a number of people. See Major Contributors to gawk for the full list.
Thanks to Michael Brennan for the Forewords.
Thanks to Patrice Dumas for the new makeinfo program.
Thanks to Karl Berry for his past work on Texinfo, and
to Gavin Smith, who continues to work to improve
the Texinfo markup language.
Robert P.J. Day, Michael Brennan, and Brian Kernighan kindly acted as reviewers for the 2015 edition of this Web page. Their feedback helped improve the final work.
I would also like to thank Brian Kernighan for his invaluable assistance during the
testing and debugging of gawk, and for his ongoing
help and advice in clarifying numerous points about the language.
We could not have done nearly as good a job on either gawk
or its documentation without his help.
Brian is in a class by himself as a programmer and technical author. I have to thank him (yet again) for his ongoing friendship and for being a role model to me for over 30 years! Having him as a reviewer is an exciting privilege. It has also been extremely humbling...
I must thank my wonderful wife, Miriam, for her patience through the many versions of this project, for her proofreading, and for sharing me with the computer. I would like to thank my parents for their love, and for the grace with which they raised and educated me. Finally, I also must acknowledge my gratitude to G-d, for the many opportunities He has sent my way, as well as for the gifts He has given me with which to take advantage of those opportunities.
Arnold Robbins
Nof Ayalon
Israel
March, 2020
Next: Running awk and gawk, Previous: Preface, Up: General Introduction [Contents][Index]
Part I:
The awk Language ¶
- Getting Started with
awk - Running
awkandgawk - Regular Expressions
- Reading Input Files
- Printing Output
- Expressions
- Patterns, Actions, and Variables
- Arrays in
awk - Functions
1 Getting Started with awk ¶
The basic function of awk is to search files for lines (or other
units of text) that contain certain patterns. When a line matches one
of the patterns, awk performs specified actions on that line.
awk continues to process input lines in this way until it reaches
the end of the input files.
Programs in awk are different from programs in most other languages,
because awk programs are data driven (i.e., you describe
the data you want to work with and then what to do when you find it).
Most other languages are procedural; you have to describe, in great
detail, every step the program should take. When working with procedural
languages, it is usually much
harder to clearly describe the data your program will process.
For this reason, awk programs are often refreshingly easy to
read and write.
When you run awk, you specify an awk program that
tells awk what to do. The program consists of a series of
rules (it may also contain function definitions,
an advanced feature that we will ignore for now;
see User-Defined Functions). Each rule specifies one
pattern to search for and one action to perform
upon finding the pattern.
Syntactically, a rule consists of a pattern followed by an
action. The action is enclosed in braces to separate it from the
pattern. Newlines usually separate rules. Therefore, an awk
program looks like this:
pattern { action }
pattern { action }
...
- How to Run
awkPrograms - Data files for the Examples
- Some Simple Examples
- An Example with Two Rules
- A More Complex Example
awkStatements Versus Lines- Other Features of
awk - When to Use
awk - Summary
Next: Data files for the Examples, Up: Getting Started with awk [Contents][Index]
1.1 How to Run awk Programs ¶
There are several ways to run an awk program. If the program is
short, it is easiest to include it in the command that runs awk,
like this:
awk 'program' input-file1 input-file2 ...
When the program is long, it is usually more convenient to put it in a file and run it with a command like this:
awk -f program-file input-file1 input-file2 ...
This section discusses both mechanisms, along with several variations of each.
- One-Shot Throwaway
awkPrograms - Running
awkWithout Input Files - Running Long Programs
- Executable
awkPrograms - Comments in
awkPrograms - Shell Quoting Issues
Next: Running awk Without Input Files, Up: How to Run awk Programs [Contents][Index]
1.1.1 One-Shot Throwaway awk Programs ¶
Once you are familiar with awk, you will often type in simple
programs the moment you want to use them. Then you can write the
program as the first argument of the awk command, like this:
awk 'program' input-file1 input-file2 ...
where program consists of a series of patterns and actions, as described earlier.
This command format instructs the shell, or command interpreter,
to start awk and use the program to process records in the
input file(s). There are single quotes around program so
the shell won’t interpret any awk characters as special shell
characters. The quotes also cause the shell to treat all of program as
a single argument for awk, and allow program to be more
than one line long.
This format is also useful for running short or medium-sized awk
programs from shell scripts, because it avoids the need for a separate
file for the awk program. A self-contained shell script is more
reliable because there are no other files to misplace.
Later in this chapter, in Some Simple Examples, we’ll see examples of several short, self-contained programs.
Next: Running Long Programs, Previous: One-Shot Throwaway awk Programs, Up: How to Run awk Programs [Contents][Index]
1.1.2 Running awk Without Input Files ¶
You can also run awk without any input files. If you type the
following command line:
awk 'program'
awk applies the program to the standard input,
which usually means whatever you type on the keyboard. This continues
until you indicate end-of-file by typing Ctrl-d.
(On non-POSIX operating systems, the end-of-file character may be different.)
As an example, the following program prints a friendly piece of advice (from Douglas Adams’s The Hitchhiker’s Guide to the Galaxy), to keep you from worrying about the complexities of computer programming:
$ awk 'BEGIN { print "Don\47t Panic!" }'
-| Don't Panic!
awk executes statements associated with BEGIN before
reading any input. If there are no other statements in your program,
as is the case here, awk just stops, instead of trying to read
input it doesn’t know how to process.
The ‘\47’ is a magic way (explained later) of getting a single quote into
the program, without having to engage in ugly shell quoting tricks.
NOTE: If you use Bash as your shell, you should execute the command ‘set +H’ before running this program interactively, to disable the C shell-style command history, which treats ‘!’ as a special character. We recommend putting this command into your personal startup file.
This next simple awk program
emulates the cat utility; it copies whatever you type on the
keyboard to its standard output (why this works is explained shortly):
$ awk '{ print }'
Now is the time for all good men
-| Now is the time for all good men
to come to the aid of their country.
-| to come to the aid of their country.
Four score and seven years ago, ...
-| Four score and seven years ago, ...
What, me worry?
-| What, me worry?
Ctrl-d
Next: Executable awk Programs, Previous: Running awk Without Input Files, Up: How to Run awk Programs [Contents][Index]
1.1.3 Running Long Programs ¶
Sometimes awk programs are very long. In these cases, it is
more convenient to put the program into a separate file. In order to tell
awk to use that file for its program, you type:
awk -f source-file input-file1 input-file2 ...
The -f instructs the awk utility to get the
awk program from the file source-file (see Command-Line Options).
Any file name can be used for source-file. For example, you
could put the program:
BEGIN { print "Don't Panic!" }
into the file advice. Then this command:
awk -f advice
does the same thing as this one:
awk 'BEGIN { print "Don\47t Panic!" }'
This was explained earlier
(see Running awk Without Input Files).
Note that you don’t usually need single quotes around the file name that you
specify with -f, because most file names don’t contain any of the shell’s
special characters. (If your file names have spaces in them, then you
will need the single quotes.) Notice that in advice, the awk
program did not have single quotes around it. The quotes are only needed
for programs that are provided on the awk command line.
(Also, placing the program in a file allows us to use a literal single quote in the program
text, instead of the magic ‘\47’.)
If you want to clearly identify an awk program file as such,
you can add the extension .awk to the file name. This doesn’t
affect the execution of the awk program but it does make
“housekeeping” easier.
Next: Comments in awk Programs, Previous: Running Long Programs, Up: How to Run awk Programs [Contents][Index]
1.1.4 Executable awk Programs ¶
Once you have learned awk, you may want to write self-contained
awk scripts, using the ‘#!’ script mechanism. You can do
this on many systems.8
For example, you could update the file advice to look like this:
#! /bin/awk -f
BEGIN { print "Don't Panic!" }
After making this file executable (with the chmod utility),
simply type ‘advice’
at the shell and the system arranges to run awk as if you had
typed ‘awk -f advice’:
$ chmod +x advice $ ./advice -| Don't Panic!
Self-contained awk scripts are useful when you want to write a
program that users can invoke without their having to know that the program is
written in awk.
| Understanding ‘#!’ |
|---|
|
The line beginning with ‘#!’ lists the full file name of an
interpreter to run and a single optional initial command-line argument
to pass to that interpreter. The operating system then runs the
interpreter with the given argument and the full argument list of the
executed program. The first argument in the list is the full file name
of the Some systems limit the length of the interpreter name to 32 characters. Often, this can be dealt with by using a symbolic link. You should not put more than one argument on the ‘#!’
line after the path to Finally, the value of |
Previous: Comments in awk Programs, Up: How to Run awk Programs [Contents][Index]
1.1.6 Shell Quoting Issues ¶
For short to medium-length awk programs, it is most convenient
to enter the program on the awk command line.
This is best done by enclosing the entire program in single quotes.
This is true whether you are entering the program interactively at
the shell prompt, or writing it as part of a larger shell script:
awk 'program text' input-file1 input-file2 ...
Once you are working with the shell, it is helpful to have a basic knowledge of shell quoting rules. The following rules apply only to POSIX-compliant, Bourne-style shells (such as Bash, the GNU Bourne-Again Shell). If you use the C shell, you’re on your own.
Before diving into the rules, we introduce a concept that appears throughout this Web page, which is that of the null, or empty, string.
The null string is character data that has no value.
In other words, it is empty. It is written in awk programs
like this: "". In the shell, it can be written using single
or double quotes: '' or "". Although the null string has
no characters in it, it does exist. For example, consider this command:
$ echo ""
Here, the echo utility receives a single argument, even
though that argument has no characters in it. In the rest of this
Web page, we use the terms null string and empty string
interchangeably. Now, on to the quoting rules:
- Quoted items can be concatenated with nonquoted items as well as with other quoted items. The shell turns everything into one argument for the command.
- Preceding any single character with a backslash (‘\’) quotes that character. The shell removes the backslash and passes the quoted character on to the command.
-
Single quotes protect everything between the opening and closing quotes.
The shell does no interpretation of the quoted text, passing it on verbatim
to the command.
It is impossible to embed a single quote inside single-quoted text.
Refer back to
Comments in
awkPrograms for an example of what happens if you try. -
Double quotes protect most things between the opening and closing quotes.
The shell does at least variable and command substitution on the quoted text.
Different shells may do additional kinds of processing on double-quoted text.
Because certain characters within double-quoted text are processed by the shell, they must be escaped within the text. Of note are the characters ‘$’, ‘`’, ‘\’, and ‘"’, all of which must be preceded by a backslash within double-quoted text if they are to be passed on literally to the program. (The shell strips the leading backslash first.) Thus, the example seen previously in Running
awkWithout Input Files:awk 'BEGIN { print "Don\47t Panic!" }'could instead be written this way:
$ awk "BEGIN { print \"Don't Panic!\" }" -| Don't Panic!Note that the single quote is not special within double quotes.
- Null strings are removed when they occur as part of a non-null
command-line argument, while explicit null objects are kept.
For example, to specify that the field separator
FSshould be set to the null string, use:awk -F "" 'program' files # correct
Don’t use this:
awk -F"" 'program' files # wrong!
In the second case,
awkattempts to use the text of the program as the value ofFS, and the first file name as the text of the program! This results in syntax errors at best, and confusing behavior at worst.
Mixing single and double quotes is difficult. You have to resort to shell quoting tricks, like this:
$ awk 'BEGIN { print "Here is a single quote <'"'"'>" }'
-| Here is a single quote <'>
This program consists of three concatenated quoted strings. The first and the third are single-quoted, and the second is double-quoted.
This can be “simplified” to:
$ awk 'BEGIN { print "Here is a single quote <'\''>" }'
-| Here is a single quote <'>
Judge for yourself which of these two is the more readable.
Another option is to use double quotes, escaping the embedded, awk-level
double quotes:
$ awk "BEGIN { print \"Here is a single quote <'>\" }"
-| Here is a single quote <'>
This option is also painful, because double quotes, backslashes, and dollar signs
are very common in more advanced awk programs.
A third option is to use the octal escape sequence equivalents (see Escape Sequences) for the single- and double-quote characters, like so:
$ awk 'BEGIN { print "Here is a single quote <\47>" }'
-| Here is a single quote <'>
$ awk 'BEGIN { print "Here is a double quote <\42>" }'
-| Here is a double quote <">
This works nicely, but you should comment clearly what the escape sequences mean.
A fourth option is to use command-line variable assignment, like this:
$ awk -v sq="'" 'BEGIN { print "Here is a single quote <" sq ">" }'
-| Here is a single quote <'>
(Here, the two string constants and the value of sq are concatenated
into a single string that is printed by print.)
If you really need both single and double quotes in your awk
program, it is probably best to move it into a separate file, where
the shell won’t be part of the picture and you can say what you mean.
Up: Shell Quoting Issues [Contents][Index]
1.1.6.1 Quoting in MS-Windows Batch Files ¶
Although this Web page generally only worries about POSIX systems and the POSIX shell, the following issue arises often enough for many users that it is worth addressing.
The “shells” on Microsoft Windows systems use the double-quote character for quoting, and make it difficult or impossible to include an escaped double quote character in a command-line script. The following example, courtesy of Jeroen Brink, shows how to escape the double quotes from this one liner script that prints all lines in a file surrounded by double quotes:
{ print "\"" $0 "\"" }
In an MS-Windows command-line the one-liner script above may be passed as follows:
gawk "{ print \"\042\" $0 \"\042\" }" file
In this example the ‘\042’ is the octal code for a double quote;
gawk converts it into a real double-quote for output by
the print statement.
In MS-Windows escaping double quotes is a little tricky because you use backslashes to escape double quotes, but backslashes themselves are not escaped in the usual way; indeed they are either duplicated or not, depending upon whether there is a subsequent double quote. The MS-Windows rule for double-quoting a string is the following:
- For each double quote in the original string, let N be the number of backslash(es) before it, N might be zero. Replace these N backslash(es) by 2*N+1 backslash(es)
- Let N be the number of backslash(es) tailing the original string, N might be zero. Replace these N backslash(es) by 2*N backslash(es)
- Surround the resulting string by double quotes.
So to double-quote the one-liner script ‘{ print "\"" $0 "\"" }’ from the previous example you would do it this way:
gawk "{ print \"\\\"\" $0 \"\\\"\" }" file
However, the use of ‘\042’ instead of ‘\\\"’ is also possible and easier to read, because backslashes that are not followed by a double quote don’t need duplication.
Next: Some Simple Examples, Previous: How to Run awk Programs, Up: Getting Started with awk [Contents][Index]
1.2 Data files for the Examples ¶
Many of the examples in this Web page take their input from two sample data files. The first, mail-list, represents a list of peoples’ names together with their email addresses and information about those people. The second data file, called inventory-shipped, contains information about monthly shipments. In both files, each line is considered to be one record.
In mail-list, each record contains the name of a person, his/her phone number, his/her email address, and a code for his/her relationship with the author of the list. The columns are aligned using spaces. An ‘A’ in the last column means that the person is an acquaintance. An ‘F’ in the last column means that the person is a friend. An ‘R’ means that the person is a relative:
Amelia 555-5553 amelia.zodiacusque@gmail.com F Anthony 555-3412 anthony.asserturo@hotmail.com A Becky 555-7685 becky.algebrarum@gmail.com A Bill 555-1675 bill.drowning@hotmail.com A Broderick 555-0542 broderick.aliquotiens@yahoo.com R Camilla 555-2912 camilla.infusarum@skynet.be R Fabius 555-1234 fabius.undevicesimus@ucb.edu F Julie 555-6699 julie.perscrutabor@skeeve.com F Martin 555-6480 martin.codicibus@hotmail.com A Samuel 555-3430 samuel.lanceolis@shu.edu A Jean-Paul 555-2127 jeanpaul.campanorum@nyu.edu R
The data file inventory-shipped represents information about shipments during the year. Each record contains the month, the number of green crates shipped, the number of red boxes shipped, the number of orange bags shipped, and the number of blue packages shipped, respectively. There are 16 entries, covering the 12 months of last year and the first four months of the current year. An empty line separates the data for the two years:
Jan 13 25 15 115 Feb 15 32 24 226 Mar 15 24 34 228 Apr 31 52 63 420 May 16 34 29 208 Jun 31 42 75 492 Jul 24 34 67 436 Aug 15 34 47 316 Sep 13 55 37 277 Oct 29 54 68 525 Nov 20 87 82 577 Dec 17 35 61 401 Jan 21 36 64 620 Feb 26 58 80 652 Mar 24 75 70 495 Apr 21 70 74 514
The sample files are included in the gawk distribution,
in the directory awklib/eg/data.
Next: An Example with Two Rules, Previous: Data files for the Examples, Up: Getting Started with awk [Contents][Index]
1.3 Some Simple Examples ¶
The following command runs a simple awk program that searches the
input file mail-list for the character string ‘li’ (a
grouping of characters is usually called a string;
the term string is based on similar usage in English, such
as “a string of pearls” or “a string of cars in a train”):
awk '/li/ { print $0 }' mail-list
When lines containing ‘li’ are found, they are printed because ‘print $0’ means print the current line. (Just ‘print’ by itself means the same thing, so we could have written that instead.)
You will notice that slashes (‘/’) surround the string ‘li’
in the awk program. The slashes indicate that ‘li’
is the pattern to search for. This type of pattern is called a
regular expression, which is covered in more detail later
(see Regular Expressions).
The pattern is allowed to match parts of words.
There are
single quotes around the awk program so that the shell won’t
interpret any of it as special shell characters.
Here is what this program prints:
$ awk '/li/ { print $0 }' mail-list
-| Amelia 555-5553 amelia.zodiacusque@gmail.com F
-| Broderick 555-0542 broderick.aliquotiens@yahoo.com R
-| Julie 555-6699 julie.perscrutabor@skeeve.com F
-| Samuel 555-3430 samuel.lanceolis@shu.edu A
In an awk rule, either the pattern or the action can be omitted,
but not both. If the pattern is omitted, then the action is performed
for every input line. If the action is omitted, the default
action is to print all lines that match the pattern.
Thus, we could leave out the action (the print statement and the
braces) in the previous example and the result would be the same:
awk prints all lines matching the pattern ‘li’. By comparison,
omitting the print statement but retaining the braces makes an
empty action that does nothing (i.e., no lines are printed).
Many practical awk programs are just a line or two long. Following is a
collection of useful, short programs to get you started. Some of these
programs contain constructs that haven’t been covered yet. (The description
of the program will give you a good idea of what is going on, but you’ll
need to read the rest of the Web page to become an awk expert!)
Most of the examples use a data file named data. This is just a
placeholder; if you use these programs yourself, substitute
your own file names for data.
Some of the following examples use the output of ‘ls -l’ as input.
ls is a system command that gives you a listing of the files in a
directory. With the -l option, this listing includes each file’s
size and the date the file was last modified. Its output looks like this:
-rw-r--r-- 1 arnold user 1933 Nov 7 13:05 Makefile -rw-r--r-- 1 arnold user 10809 Nov 7 13:03 awk.h -rw-r--r-- 1 arnold user 983 Apr 13 12:14 awk.tab.h -rw-r--r-- 1 arnold user 31869 Jun 15 12:20 awkgram.y -rw-r--r-- 1 arnold user 22414 Nov 7 13:03 awk1.c -rw-r--r-- 1 arnold user 37455 Nov 7 13:03 awk2.c -rw-r--r-- 1 arnold user 27511 Dec 9 13:07 awk3.c -rw-r--r-- 1 arnold user 7989 Nov 7 13:03 awk4.c
The first field contains read-write permissions, the second field contains the number of links to the file, and the third field identifies the file’s owner. The fourth field identifies the file’s group. The fifth field contains the file’s size in bytes. The sixth, seventh, and eighth fields contain the month, day, and time, respectively, that the file was last modified. Finally, the ninth field contains the file name.
For future reference, note that there is often more than
one way to do things in awk. At some point, you may want
to look back at these examples and see if
you can come up with different ways to do the same things shown here:
- Print every line that is longer than 80 characters:
awk 'length($0) > 80' data
The sole rule has a relational expression as its pattern and has no action—so it uses the default action, printing the record.
- Print the length of the longest input line:
awk '{ if (length($0) > max) max = length($0) } END { print max }' dataThe code associated with
ENDexecutes after all input has been read; it’s the other side of the coin toBEGIN. -
Print the length of the longest line in data:
expand data | awk '{ if (x < length($0)) x = length($0) } END { print "maximum line length is " x }'This example differs slightly from the previous one: the input is processed by the
expandutility to change TABs into spaces, so the widths compared are actually the right-margin columns, as opposed to the number of input characters on each line. - Print every line that has at least one field:
awk 'NF > 0' data
This is an easy way to delete blank lines from a file (or rather, to create a new file similar to the old file but from which the blank lines have been removed).
- Print seven random numbers from 0 to 100, inclusive:
awk 'BEGIN { for (i = 1; i <= 7; i++) print int(101 * rand()) }' - Print the total number of bytes used by files:
ls -l files | awk '{ x += $5 } END { print "total bytes: " x }' - Print the total number of kilobytes used by files:
ls -l files | awk '{ x += $5 } END { print "total K-bytes:", x / 1024 }' - Print a sorted list of the login names of all users:
awk -F: '{ print $1 }' /etc/passwd | sort - Count the lines in a file:
awk 'END { print NR }' data - Print the even-numbered lines in the data file:
awk 'NR % 2 == 0' data
If you used the expression ‘NR % 2 == 1’ instead, the program would print the odd-numbered lines.
Next: A More Complex Example, Previous: Some Simple Examples, Up: Getting Started with awk [Contents][Index]
1.4 An Example with Two Rules ¶
The awk utility reads the input files one line at a
time. For each line, awk tries the patterns of each rule.
If several patterns match, then several actions execute in the order in
which they appear in the awk program. If no patterns match, then
no actions run.
After processing all the rules that match the line (and perhaps there are none),
awk reads the next line. (However,
see The next Statement
and also see The nextfile Statement.)
This continues until the program reaches the end of the file.
For example, the following awk program contains two rules:
/12/ { print $0 }
/21/ { print $0 }
The first rule has the string ‘12’ as the pattern and ‘print $0’ as the action. The second rule has the string ‘21’ as the pattern and also has ‘print $0’ as the action. Each rule’s action is enclosed in its own pair of braces.
This program prints every line that contains the string ‘12’ or the string ‘21’. If a line contains both strings, it is printed twice, once by each rule.
This is what happens if we run this program on our two sample data files, mail-list and inventory-shipped:
$ awk '/12/ { print $0 }
> /21/ { print $0 }' mail-list inventory-shipped
-| Anthony 555-3412 anthony.asserturo@hotmail.com A
-| Camilla 555-2912 camilla.infusarum@skynet.be R
-| Fabius 555-1234 fabius.undevicesimus@ucb.edu F
-| Jean-Paul 555-2127 jeanpaul.campanorum@nyu.edu R
-| Jean-Paul 555-2127 jeanpaul.campanorum@nyu.edu R
-| Jan 21 36 64 620
-| Apr 21 70 74 514
Note how the line beginning with ‘Jean-Paul’ in mail-list was printed twice, once for each rule.
Next: awk Statements Versus Lines, Previous: An Example with Two Rules, Up: Getting Started with awk [Contents][Index]
1.5 A More Complex Example ¶
Now that we’ve mastered some simple tasks, let’s look at
what typical awk
programs do. This example shows how awk can be used to
summarize, select, and rearrange the output of another utility. It uses
features that haven’t been covered yet, so don’t worry if you don’t
understand all the details:
ls -l | awk '$6 == "Nov" { sum += $5 }
END { print sum }'
This command prints the total number of bytes in all the files in the current directory that were last modified in November (of any year).
As a reminder, the output of ‘ls -l’ gives you a listing of the files in a directory, including each file’s size and the date the file was last modified. The first field contains read-write permissions, the second field contains the number of links to the file, and the third field identifies the file’s owner. The fourth field identifies the file’s group. The fifth field contains the file’s size in bytes. The sixth, seventh, and eighth fields contain the month, day, and time, respectively, that the file was last modified. Finally, the ninth field contains the file name.
The ‘$6 == "Nov"’ in our awk program is an expression that
tests whether the sixth field of the output from ‘ls -l’
matches the string ‘Nov’. Each time a line has the string
‘Nov’ for its sixth field, awk performs the action
‘sum += $5’. This adds the fifth field (the file’s size) to the variable
sum. As a result, when awk has finished reading all the
input lines, sum is the total of the sizes of the files whose
lines matched the pattern. (This works because awk variables
are automatically initialized to zero.)
After the last line of output from ls has been processed, the
END rule executes and prints the value of sum.
In this example, the value of sum is 80600.
These more advanced awk techniques are covered in later
sections
(see Actions). Before you can move on to more
advanced awk programming, you have to know how awk interprets
your input and displays your output. By manipulating fields and using
print statements, you can produce some very useful and
impressive-looking reports.
Next: Other Features of awk, Previous: A More Complex Example, Up: Getting Started with awk [Contents][Index]
1.6 awk Statements Versus Lines ¶
Most often, each line in an awk program is a separate statement or
separate rule, like this:
awk '/12/ { print $0 }
/21/ { print $0 }' mail-list inventory-shipped
However, gawk ignores newlines after any of the following
symbols and keywords:
, { ? : || && do else
A newline at any other point is considered the end of the statement.9
If you would like to split a single statement into two lines at a point where a newline would terminate it, you can continue it by ending the first line with a backslash character (‘\’). The backslash must be the final character on the line in order to be recognized as a continuation character. A backslash followed by a newline is allowed anywhere in the statement, even in the middle of a string or regular expression. For example:
awk '/This regular expression is too long, so continue it\
on the next line/ { print $1 }'
We have generally not used backslash continuation in our sample programs.
gawk places no limit on the
length of a line, so backslash continuation is never strictly necessary;
it just makes programs more readable. For this same reason, as well as
for clarity, we have kept most statements short in the programs
presented throughout the Web page.
Backslash continuation is
most useful when your awk program is in a separate source file
instead of entered from the command line. You should also note that
many awk implementations are more particular about where you
may use backslash continuation. For example, they may not allow you to
split a string constant using backslash continuation. Thus, for maximum
portability of your awk programs, it is best not to split your
lines in the middle of a regular expression or a string.
CAUTION: Backslash continuation does not work as described with the C shell. It works for
awkprograms in files and for one-shot programs, provided you are using a POSIX-compliant shell, such as the Unix Bourne shell or Bash. But the C shell behaves differently! There you must use two backslashes in a row, followed by a newline. Note also that when using the C shell, every newline in yourawkprogram must be escaped with a backslash. To illustrate:% awk 'BEGIN { \ ? print \\ ? "hello, world" \ ? }' -| hello, worldHere, the ‘%’ and ‘?’ are the C shell’s primary and secondary prompts, analogous to the standard shell’s ‘$’ and ‘>’.
Compare the previous example to how it is done with a POSIX-compliant shell:
$ awk 'BEGIN { > print \ > "hello, world" > }' -| hello, world
awk is a line-oriented language. Each rule’s action has to
begin on the same line as the pattern. To have the pattern and action
on separate lines, you must use backslash continuation; there
is no other option.
Another thing to keep in mind is that backslash continuation and
comments do not mix. As soon as awk sees the ‘#’ that
starts a comment, it ignores everything on the rest of the
line. For example:
$ gawk 'BEGIN { print "don\47t panic" # a friendly \
> BEGIN rule
> }'
error→ gawk: cmd. line:2: BEGIN rule
error→ gawk: cmd. line:2: ^ syntax error
In this case, it looks like the backslash would continue the comment onto the
next line. However, the backslash-newline combination is never even
noticed because it is “hidden” inside the comment. Thus, the
BEGIN is noted as a syntax error.
Backslash continuation comes into play in an additional, unexpected situation. Consider:
gawk -F'\ a' '...'
This command line assigns a value to FS. But what value?
There are several possibilities, and in fact different versions of
awk do different things. gawk treats this as
if it were written:
BEGIN { FS = "\
a"
}
...
In short, the backslash and newline are removed, assigning "a"
to FS. This same treatment applies to variable assignments
made with the -v option (see Command-Line Options)
and to regular command-line variable assignments (see Assigning Variables on the Command Line).
If you’re interested, see
https://lists.gnu.org/archive/html/bug-gawk/2022-10/msg00025.html
for a source code patch that allows lines to be continued when inside
parentheses. This patch was not added to gawk since it would
quietly decrease the portability of awk programs.
When awk statements within one rule are short, you might want to put
more than one of them on a line. This is accomplished by separating the statements
with a semicolon (‘;’).
This also applies to the rules themselves.
Thus, the program shown at the start of this section
could also be written this way:
/12/ { print $0 } ; /21/ { print $0 }
NOTE: The requirement that states that rules on the same line must be separated with a semicolon was not in the original
awklanguage; it was added for consistency with the treatment of statements within an action.
Next: When to Use awk, Previous: awk Statements Versus Lines, Up: Getting Started with awk [Contents][Index]
1.7 Other Features of awk ¶
The awk language provides a number of predefined, or
built-in, variables that your programs can use to get information
from awk. There are other variables your program can set
as well to control how awk processes your data.
In addition, awk provides a number of built-in functions for doing
common computational and string-related operations.
gawk provides built-in functions for working with timestamps,
performing bit manipulation, for runtime string translation (internationalization),
determining the type of a variable,
and array sorting.
As we develop our presentation of the awk language, we will introduce
most of the variables and many of the functions. They are described
systematically in Predefined Variables and in
Built-in Functions.
Next: Summary, Previous: Other Features of awk, Up: Getting Started with awk [Contents][Index]
1.8 When to Use awk ¶
Now that you’ve seen some of what awk can do,
you might wonder how awk could be useful for you. By using
utility programs, advanced patterns, field separators, arithmetic
statements, and other selection criteria, you can produce much more
complex output. The awk language is very useful for producing
reports from large amounts of raw data, such as summarizing information
from the output of other utility programs like ls.
(See A More Complex Example.)
Programs written with awk are usually much smaller than they would
be in other languages. This makes awk programs easy to compose and
use. Often, awk programs can be quickly composed at your keyboard,
used once, and thrown away. Because awk programs are interpreted, you
can avoid the (usually lengthy) compilation part of the typical
edit-compile-test-debug cycle of software development.
Complex programs have been written in awk, including a complete
retargetable assembler for
eight-bit microprocessors (see Glossary, for more information),
and a microcode assembler for a special-purpose Prolog
computer.
The original awk’s capabilities were strained by tasks
of such complexity, but modern versions are more capable.
If you find yourself writing awk scripts of more than, say,
a few hundred lines, you might consider using a different programming
language. The shell is good at string and pattern matching; in addition,
it allows powerful use of the system utilities. Python offers a nice
balance between high-level ease of programming and access to system
facilities.10
Previous: When to Use awk, Up: Getting Started with awk [Contents][Index]
1.9 Summary ¶
- Programs in
awkconsist of pattern–action pairs. - An action without a pattern always runs. The default action for a pattern without one is ‘{ print $0 }’.
- Use either
‘awk 'program' files’
or
‘awk -f program-file files’
to run
awk. - You may use the special ‘#!’ header line to create
awkprograms that are directly executable. - Comments in
awkprograms start with ‘#’ and continue to the end of the same line. - Be aware of quoting issues when writing
awkprograms as part of a larger shell script (or MS-Windows batch file). - You may use backslash continuation to continue a source line.
Lines are automatically continued after
a comma, open brace, question mark, colon,
‘||’, ‘&&’,
do, andelse.
Next: Regular Expressions, Previous: Getting Started with awk, Up: General Introduction [Contents][Index]
2 Running awk and gawk ¶
This chapter covers how to run awk, both POSIX-standard
and gawk-specific command-line options, and what
awk and
gawk do with nonoption arguments.
It then proceeds to cover how gawk searches for source files,
reading standard input along with other files, gawk’s
environment variables, gawk’s exit status, using include files,
and obsolete and undocumented options and/or features.
Many of the options and features described here are discussed in more detail later in the Web page; feel free to skip over things in this chapter that don’t interest you right now.
- Invoking
awk - Command-Line Options
- Other Command-Line Arguments
- Naming Standard Input
- The Environment Variables
gawkUses gawk’s Exit Status- Including Other Files into Your Program
- Loading Dynamic Extensions into Your Program
- Obsolete Options and/or Features
- Undocumented Options and Features
- Summary
Next: Command-Line Options, Up: Running awk and gawk [Contents][Index]
2.1 Invoking awk ¶
There are two ways to run awk—with an explicit program or with
one or more program files. Here are templates for both of them; items
enclosed in […] in these templates are optional:
awk[options] -f progfile [--] file ...awk[options] [--]'program'file ...
In addition to traditional one-letter POSIX-style options, gawk also
supports GNU long options.
It is possible to invoke awk with an empty program:
awk '' datafile1 datafile2
Doing so makes little sense, though; awk exits
silently when given an empty program.
(d.c.)
If --lint has
been specified on the command line, gawk issues a
warning that the program is empty.
Next: Other Command-Line Arguments, Previous: Invoking awk, Up: Running awk and gawk [Contents][Index]
2.2 Command-Line Options ¶
Options begin with a dash and consist of a single character. GNU-style long options consist of two dashes and a keyword. The keyword can be abbreviated, as long as the abbreviation allows the option to be uniquely identified. If the option takes an argument, either the keyword is immediately followed by an equals sign (‘=’) and the argument’s value, or the keyword and the argument’s value are separated by whitespace (spaces or TABs). If a particular option with a value is given more than once, it is (usually) the last value that counts.
Each long option for gawk has a corresponding
POSIX-style short option.
The long and short options are
interchangeable in all contexts.
The following list describes options mandated by the POSIX standard:
-
-F fs¶ --field-separator fsSet the
FSvariable to fs (see Specifying How Fields Are Separated).-
-f source-file¶ --file source-fileRead the
awkprogram source from source-file instead of in the first nonoption argument. This option may be given multiple times; theawkprogram consists of the concatenation of the contents of each specified source-file.Files named with -f are treated as if they had ‘@namespace "awk"’ at their beginning. See Changing The Namespace, for more information on this advanced feature.
-
-v var=val¶ --assign var=valSet the variable var to the value val before execution of the program begins. Such variable values are available inside the
BEGINrule (see Other Command-Line Arguments).The -v option can only set one variable, but it can be used more than once, setting another variable each time, like this: ‘awk -v foo=1 -v bar=2 …’.
CAUTION: Using -v to set the values of the built-in variables may lead to surprising results.
awkwill reset the values of those variables as it needs to, possibly ignoring any initial value you may have given.-W gawk-opt¶Provide an implementation-specific option. This is the POSIX convention for providing implementation-specific options. These options also have corresponding GNU-style long options. Note that the long options may be abbreviated, as long as the abbreviations remain unique. The full list of
gawk-specific options is provided next.--¶-
Signal the end of the command-line options. The following arguments are not treated as options even if they begin with ‘-’. This interpretation of -- follows the POSIX argument parsing conventions.
This is useful if you have file names that start with ‘-’, or in shell scripts, if you have file names that will be specified by the user that could start with ‘-’. It is also useful for passing options on to the
awkprogram; see Processing Command-Line Options.
The following list describes gawk-specific options:
- -b ¶
- --characters-as-bytes
Cause
gawkto treat all input data as single-byte characters. In addition, all output written withprintorprintfis treated as single-byte characters.Normally,
gawkfollows the POSIX standard and attempts to process its input data according to the current locale (see Where You Are Makes a Difference). This can often involve converting multibyte characters into wide characters (internally), and can lead to problems or confusion if the input data does not contain valid multibyte characters. This option is an easy way to tellgawk, “Hands off my data!”- -c ¶
- --traditional
Specify compatibility mode, in which the GNU extensions to the
awklanguage are disabled, so thatgawkbehaves just like BWKawk. See Extensions ingawkNot in POSIXawk, which summarizes the extensions. Also see Downward Compatibility and Debugging.- -C ¶
- --copyright
Print the short version of the General Public License and then exit.
- -d[file] ¶
- --dump-variables[
=file] -
Print a sorted list of global variables, their types, and final values to file. If no file is provided, print this list to a file named awkvars.out in the current directory. No space is allowed between the -d and file, if file is supplied.
Having a list of all global variables is a good way to look for typographical errors in your programs. You would also use this option if you have a large program with a lot of functions, and you want to be sure that your functions don’t inadvertently use global variables that you meant to be local. (This is a particularly easy mistake to make with simple variable names like
i,j, etc.) - -D[file] ¶
- --debug[
=file] Enable debugging of
awkprograms (see Introduction to thegawkDebugger). By default, the debugger reads commands interactively from the keyboard (standard input). The optional file argument allows you to specify a file with a list of commands for the debugger to execute noninteractively. No space is allowed between the -D and file, if file is supplied.- -e program-text ¶
- --source program-text
Provide program source code in the program-text. This option allows you to mix source code in files with source code that you enter on the command line. This is particularly useful when you have library functions that you want to use from your command-line programs (see The
AWKPATHEnvironment Variable).Note that
gawktreats each string as if it ended with a newline character (even if it doesn’t). This makes building the total program easier.CAUTION: Prior to version 5.0, there was no requirement that each program-text be a full syntactic unit. I.e., the following worked:
$ gawk -e 'BEGIN { a = 5 ;' -e 'print a }' -| 5However, this is no longer true. If you have any scripts that rely upon this feature, you should revise them.
This is because each program-text is treated as if it had ‘@namespace "awk"’ at its beginning. See Changing The Namespace, for more information.
- -E file ¶
- --exec file
-
Similar to -f, read
awkprogram text from file. There are two differences from -f:- This option terminates option processing; anything
else on the command line is passed on directly to the
awkprogram. - Command-line variable assignments of the form ‘var=value’ are disallowed.
This option is particularly necessary for World Wide Web CGI applications that pass arguments through the URL; using this option prevents a malicious (or other) user from passing in options, assignments, or
awksource code (via -e) to the CGI application.11 This option should be used with ‘#!’ scripts (see ExecutableawkPrograms), like so:#! /usr/local/bin/gawk -E awk program here ...
- This option terminates option processing; anything
else on the command line is passed on directly to the
- -g ¶
- --gen-pot
-
Analyze the source program and generate a GNU
gettextportable object template file on standard output for all string constants that have been marked for translation. See Internationalization withgawk, for information about this option. - -h ¶
- --help
-
Print a “usage” message summarizing the short- and long-style options that
gawkaccepts and then exit. - -i source-file ¶
- --include source-file
-
Read an
awksource library from source-file. This option is completely equivalent to using the@includedirective inside your program. It is very similar to the -f option, but there are two important differences. First, when -i is used, the program source is not loaded if it has been previously loaded, whereas with -f,gawkalways loads the file. Second, because this option is intended to be used with code libraries,gawkdoes not recognize such files as constituting main program input. Thus, after processing an -i argument,gawkstill expects to find the main source code via the -f option or on the command line.Files named with -i are treated as if they had ‘@namespace "awk"’ at their beginning. See Changing The Namespace, for more information.
- -I ¶
- --trace
Print the internal byte code names as they are executed when running the program. The trace is printed to standard error. Each “op code” is preceded by a
+sign in the output.- -k ¶
- --csv
-
Enable special processing for files with comma separated values (CSV). See Working With Comma Separated Value Files. This option cannot be used with --posix. Attempting to do causes a fatal error.
- -l ext ¶
- --load ext
-
Load a dynamic extension named ext. Extensions are stored as system shared libraries. This option searches for the library using the
AWKLIBPATHenvironment variable. The correct library suffix for your platform will be supplied by default, so it need not be specified in the extension name. The extension initialization routine should be nameddl_load(). An alternative is to use the@loaddirective inside the program to load a shared library. This advanced feature is described in detail in Writing Extensions forgawk. - -L[value] ¶
- --lint[
=value] -
Warn about constructs that are dubious or nonportable to other
awkimplementations. No space is allowed between the -L and value, if value is supplied. Some warnings are issued whengawkfirst reads your program. Others are issued at runtime, as your program executes. The optional argument may be one of the following:fatalCause lint warnings become fatal errors. This may be drastic, but its use will certainly encourage the development of cleaner
awkprograms.invalidOnly issue warnings about things that are actually invalid are issued. (This is not fully implemented yet.)
no-extDisable warnings about
gawkextensions.
Some warnings are only printed once, even if the dubious constructs they warn about occur multiple times in your
awkprogram. Thus, when eliminating problems pointed out by --lint, you should take care to search for all occurrences of each inappropriate construct. Asawkprograms are usually short, doing so is not burdensome. - -M ¶
- --bignum
Select arbitrary-precision arithmetic on numbers. This option has no effect if
gawkis not compiled to use the GNU MPFR and MP libraries (see Arithmetic and Arbitrary-Precision Arithmetic withgawk).As of version 5.2, the arbitrary precision arithmetic features in
gawkare “on parole.” The primary maintainer is no longer willing to support this feature, but another member of the development team has stepped up to take it over. As long as this situation remains stable, MPFR will be supported. If it changes, the MPFR support will be removed fromgawk.- -n ¶
- --non-decimal-data
Enable automatic interpretation of octal and hexadecimal values in input data (see Allowing Nondecimal Input Data).
CAUTION: This option can severely break old programs. Use with care.
- -N ¶
- --use-lc-numeric
Force the use of the locale’s decimal point character when parsing numeric input data (see Where You Are Makes a Difference).
- -o[file] ¶
- --pretty-print[
=file] Enable pretty-printing of
awkprograms. Implies --no-optimize. By default, the output program is created in a file named awkprof.out (see Profiling YourawkPrograms). The optional file argument allows you to specify a different file name for the output. No space is allowed between the -o and file, if file is supplied.NOTE: In the past, this option would also execute your program. This is no longer the case.
- -O ¶
- --optimize
Enable
gawk’s default optimizations on the internal representation of the program. At the moment, this includes just simple constant folding.Optimization is enabled by default. This option remains primarily for backwards compatibility. However, it may be used to cancel the effect of an earlier -s option (see later in this list).
- -p[file] ¶
- --profile[
=file] Enable profiling of
awkprograms (see Profiling YourawkPrograms). Implies --no-optimize. By default, profiles are created in a file named awkprof.out. The optional file argument allows you to specify a different file name for the profile file. No space is allowed between the -p and file, if file is supplied.The profile contains execution counts for each statement in the program in the left margin, and function call counts for each function.
- -P ¶
- --posix
Operate in strict POSIX mode. This disables all
gawkextensions (just like --traditional) and disables all extensions not allowed by POSIX. See Common Extensions Summary for a summary of the extensions ingawkthat are disabled by this option. Also, the following additional restrictions apply:- Newlines are not allowed after ‘?’ or ‘:’ (see Conditional Expressions).
- Specifying ‘-Ft’ on the command line does not set the value
of
FSto be a single TAB character (see Specifying How Fields Are Separated). - The locale’s decimal point character is used for parsing input data (see Where You Are Makes a Difference).
If you supply both --traditional and --posix on the command line, --posix takes precedence.
gawkissues a warning if both options are supplied.- -r ¶
- --re-interval
Allow interval expressions (see Regular Expression Operators) in regexps. This is now
gawk’s default behavior. Nevertheless, this option remains for backward compatibility.- -s ¶
- --no-optimize
Disable
gawk’s default optimizations on the internal representation of the program.- -S ¶
- --sandbox
Disable the
system()function, input redirections withgetline, output redirections withprintandprintf, and dynamic extensions. Also, disallow adding file names toARGVthat were not there whengawkstarted running. This is particularly useful when you want to runawkscripts from questionable sources and need to make sure the scripts can’t access your system (other than the specified input data files).- -t ¶
- --lint-old
Warn about constructs that are not available in the original version of
awkfrom Version 7 Unix (see Major Changes Between V7 and SVR3.1).- -V ¶
- --version
Print version information for this particular copy of
gawk. This allows you to determine if your copy ofgawkis up to date with respect to whatever the Free Software Foundation is currently distributing. It is also useful for bug reports (see Reporting Problems and Bugs).--Mark the end of all options. Any command-line arguments following
--are placed inARGV, even if they start with a minus sign.
In compatibility mode, as long as program text has been supplied, any other options are flagged as invalid with a warning message but are otherwise ignored.
In compatibility mode, as a special case, if the value of fs supplied
to the -F option is ‘t’, then FS is set to the TAB
character ("\t"). This is true only for --traditional and not
for --posix
(see Specifying How Fields Are Separated).
The -f option may be used more than once on the command line.
If it is, awk reads its program source from all of the named files, as
if they had been concatenated together into one big file. This is
useful for creating libraries of awk functions. These functions
can be written once and then retrieved from a standard place, instead
of having to be included in each individual program.
The -i option is similar in this regard.
(As mentioned in
Function Definition Syntax,
function names must be unique.)
With standard awk, library functions can still be used, even
if the program is entered at the keyboard,
by specifying ‘-f /dev/tty’. After typing your program,
type Ctrl-d (the end-of-file character) to terminate it.
(You may also use ‘-f -’ to read program source from the standard
input, but then you will not be able to also use the standard input as a
source of data.)
Because it is clumsy using the standard awk mechanisms to mix
source file and command-line awk programs, gawk
provides the -e option. This does not require you to
preempt the standard input for your source code, and it allows you to easily
mix command-line and library source code (see The AWKPATH Environment Variable).
As with -f, the -e and -i
options may also be used multiple times on the command line.
If no -f option (or -e option for gawk)
is specified, then awk uses the first nonoption command-line
argument as the text of the program source code. Arguments on
the command line that follow the program text are entered into the
ARGV array; awk does not continue to parse the
command line looking for options.
If the environment variable POSIXLY_CORRECT exists,
then gawk behaves in strict POSIX mode, exactly as if
you had supplied --posix.
Many GNU programs look for this environment variable to suppress
extensions that conflict with POSIX, but gawk behaves
differently: it suppresses all extensions, even those that do not
conflict with POSIX, and behaves in
strict POSIX mode. If --lint is supplied on the command line
and gawk turns on POSIX mode because of POSIXLY_CORRECT,
then it issues a warning message indicating that POSIX
mode is in effect.
You would typically set this variable in your shell’s startup file.
For a Bourne-compatible shell (such as Bash), you would add these
lines to the .profile file in your home directory:
POSIXLY_CORRECT=true export POSIXLY_CORRECT
For a C shell-compatible shell,12 you would add this line to the .login file in your home directory:
setenv POSIXLY_CORRECT true
Having POSIXLY_CORRECT set is not recommended for daily use,
but it is good for testing the portability of your programs to other
environments.
Next: Naming Standard Input, Previous: Command-Line Options, Up: Running awk and gawk [Contents][Index]
2.3 Other Command-Line Arguments ¶
Any additional arguments on the command line are normally treated as
input files to be processed in the order specified. However, an
argument that has the form var=value, assigns
the value value to the variable var—it does not specify a
file at all. (See Assigning Variables on the Command Line.) In the following example,
‘count=1’ is a variable assignment, not a file name:
awk -f program.awk file1 count=1 file2
As a side point, should you really need to have awk
process a file named count=1 (or any file whose name looks like
a variable assignment), precede the file name with ‘./’, like so:
awk -f program.awk file1 ./count=1 file2
All the command-line arguments are made available to your awk program in the
ARGV array (see Predefined Variables). Command-line options
and the program text (if present) are omitted from ARGV.
All other arguments, including variable assignments, are
included. As each element of ARGV is processed, gawk
sets ARGIND to the index in ARGV of the
current element. (gawk makes the full command line,
including program text and options, available in PROCINFO["argv"];
see Built-in Variables That Convey Information.)
Changing ARGC and ARGV in your awk program lets
you control how awk processes the input files; this is described
in more detail in Using ARGC and ARGV.
The distinction between file name arguments and variable-assignment
arguments is made when awk is about to open the next input file.
At that point in execution, it checks the file name to see whether
it is really a variable assignment; if so, awk sets the variable
instead of reading a file.
Therefore, the variables actually receive the given values after all
previously specified files have been read. In particular, the values of
variables assigned in this fashion are not available inside a
BEGIN rule
(see The BEGIN and END Special Patterns),
because such rules are run before awk begins scanning the argument list.
The variable values given on the command line are processed for escape sequences (see Escape Sequences). (d.c.)
In some very early implementations of awk, when a variable assignment
occurred before any file names, the assignment would happen before
the BEGIN rule was executed. awk’s behavior was thus
inconsistent; some command-line assignments were available inside the
BEGIN rule, while others were not. Unfortunately,
some applications came to depend
upon this “feature.” When awk was changed to be more consistent,
the -v option was added to accommodate applications that depended
upon the old behavior.
The variable assignment feature is most useful for assigning to variables
such as RS, OFS, and ORS, which control input and
output formats, before scanning the data files. It is also useful for
controlling state if multiple passes are needed over a data file. For
example:
awk 'pass == 1 { pass 1 stuff }
pass == 2 { pass 2 stuff }' pass=1 mydata pass=2 mydata
Given the variable assignment feature, the -F option for setting
the value of FS is not
strictly necessary. It remains for historical compatibility.
Next: The Environment Variables gawk Uses, Previous: Other Command-Line Arguments, Up: Running awk and gawk [Contents][Index]
2.4 Naming Standard Input ¶
Often, you may wish to read standard input together with other files. For example, you may wish to read one file, read standard input coming from a pipe, and then read another file.
The way to name the standard input, with all versions of awk,
is to use a single, standalone minus sign or dash, ‘-’. For example:
some_command | awk -f myprog.awk file1 - file2
Here, awk first reads file1, then it reads
the output of some_command, and finally it reads
file2.
You may also use "-" to name standard input when reading
files with getline (see Using getline from a File).
And, you can even use "-" with the -f option
to read program source code from standard input (see Command-Line Options).
In addition, gawk allows you to specify the special
file name /dev/stdin, both on the command line and
with getline.
Some other versions of awk also support this, but it
is not standard.
(Some operating systems provide a /dev/stdin file
in the filesystem; however, gawk always processes
this file name itself.)
Next: gawk’s Exit Status, Previous: Naming Standard Input, Up: Running awk and gawk [Contents][Index]
2.5 The Environment Variables gawk Uses ¶
A number of environment variables influence how gawk
behaves.
Next: The AWKLIBPATH Environment Variable, Up: The Environment Variables gawk Uses [Contents][Index]
2.5.1 The AWKPATH Environment Variable ¶
In most awk
implementations, you must supply a precise pathname for each program
file, unless the file is in the current directory.
But with gawk, if the file name supplied to the -f
or -i options
does not contain a directory separator ‘/’, then gawk searches a list of
directories (called the search path) one by one, looking for a
file with the specified name.
The search path is a string consisting of directory names
separated by colons.13
gawk gets its search path from the
AWKPATH environment variable. If that variable does not exist,
or if it has an empty value,
gawk uses a default path (described shortly).
The search path feature is particularly helpful for building libraries
of useful awk functions. The library files can be placed in a
standard directory in the default path and then specified on
the command line with a short file name. Otherwise, you would have to
type the full file name for each file.
By using the -i or -f options, your command-line
awk programs can use facilities in awk library files
(see A Library of awk Functions).
Path searching is always done, even if gawk is in compatibility mode.
This is true for both --traditional and --posix.
See Command-Line Options.
If the source code file is not found after the initial search, the path is searched again after adding the suffix ‘.awk’ to the file name.
gawk’s path search mechanism is similar
to the shell’s.
(See The Bourne-Again SHell manual.)
It treats a null entry in the path as indicating the current
directory.
(A null entry is indicated by starting or ending the path with a
colon or by placing two colons next to each other [‘::’].)
NOTE: To include the current directory in the path, either place . as an entry in the path or write a null entry in the path.
Different past versions of
gawkwould also look explicitly in the current directory, either before or after the path search. As of version 4.1.2, this no longer happens; if you wish to look in the current directory, you must include . either as a separate entry or as a null entry in the search path.
The default value for AWKPATH is
‘.:/usr/local/share/awk’.14 Since . is included at the beginning, gawk
searches first in the current directory and then in /usr/local/share/awk.
In practice, this means that you will rarely need to change the
value of AWKPATH.
See Shell Startup Files, for information on functions that help to
manipulate the AWKPATH variable.
gawk places the value of the search path that it used into
ENVIRON["AWKPATH"]. This provides access to the actual search
path value from within an awk program.
Although you can change ENVIRON["AWKPATH"] within your awk
program, this has no effect on the running program’s behavior. This makes
sense: the AWKPATH environment variable is used to find the program
source files. Once your program is running, all the files have been
found, and gawk no longer needs to use AWKPATH.
Next: Other Environment Variables, Previous: The AWKPATH Environment Variable, Up: The Environment Variables gawk Uses [Contents][Index]
2.5.2 The AWKLIBPATH Environment Variable ¶
The AWKLIBPATH environment variable is similar to the AWKPATH
variable, but it is used to search for loadable extensions (stored as
system shared libraries) specified with the -l option rather
than for source files. If the extension is not found, the path is
searched again after adding the appropriate shared library suffix for
the platform. For example, on GNU/Linux systems, the suffix ‘.so’
is used. The search path specified is also used for extensions loaded
via the @load directive (see Loading Dynamic Extensions into Your Program).
If AWKLIBPATH does not exist in the environment, or if it has
an empty value, gawk uses a default path; this
is typically ‘/usr/local/lib/gawk’, although it can vary depending
upon how gawk was built.15
See Shell Startup Files, for information on functions that help to
manipulate the AWKLIBPATH variable.
gawk places the value of the search path that it used into
ENVIRON["AWKLIBPATH"]. This provides access to the actual search
path value from within an awk program.
Although you can change ENVIRON["AWKLIBPATH"] within your
awk program, this has no effect on the running program’s
behavior. This makes sense: the AWKLIBPATH environment variable
is used to find any requested extensions, and they are loaded before
the program starts to run. Once your program is running, all the
extensions have been found, and gawk no longer needs to use
AWKLIBPATH.
Previous: The AWKLIBPATH Environment Variable, Up: The Environment Variables gawk Uses [Contents][Index]
2.5.3 Other Environment Variables ¶
A number of other environment variables affect gawk’s
behavior, but they are more specialized. Those in the following
list are meant to be used by regular users:
GAWK_MSEC_SLEEPSpecifies the interval between connection retries, in milliseconds. On systems that do not support the
usleep()system call, the value is rounded up to an integral number of seconds.GAWK_PERSIST_FILESpecifies the backing file to use for persistent storage of
gawk’s variables and arrays. See Preserving Data Between Runs.GAWK_READ_TIMEOUTSpecifies the time, in milliseconds, for
gawkto wait for input before returning with an error. See Reading Input with a Timeout.GAWK_SOCK_RETRIESControls the number of times
gawkattempts to retry a two-way TCP/IP (socket) connection before giving up. See Usinggawkfor Network Programming. Note that when nonfatal I/O is enabled (see Enabling Nonfatal Output),gawkonly tries to open a TCP/IP socket once.PMA_VERBOSITYControls the verbosity of the persistent memory allocator. See Preserving Data Between Runs.
POSIXLY_CORRECTCauses
gawkto switch to POSIX-compatibility mode, disabling all traditional and GNU extensions. See Command-Line Options.
The environment variables in the following list are meant
for use by the gawk developers for testing and tuning.
They are subject to change. The variables are:
AWKBUFSIZEThis variable only affects
gawkon POSIX-compliant systems. With a value of ‘exact’,gawkuses the size of each input file as the size of the memory buffer to allocate for I/O. Otherwise, the value should be a number, andgawkuses that number as the size of the buffer to allocate. (When this variable is not set,gawkuses the smaller of the file’s size and the “default” blocksize, which is usually the filesystem’s I/O blocksize.)AWK_HASHIf this variable exists with a value of ‘gst’,
gawkswitches to using the hash function from GNU Smalltalk for managing arrays. With a value of ‘fnv1a’,gawkuses the FNV1-A hash function. These functions may be marginally faster than the standard function.AWKREADFUNCIf this variable exists,
gawkswitches to reading source files one line at a time, instead of reading in blocks. This exists for debugging problems on filesystems on non-POSIX operating systems where I/O is performed in records, not in blocks.GAWK_MSG_SRCIf this variable exists,
gawkincludes the file name and line number within thegawksource code from which warning and/or fatal messages are generated. Its purpose is to help isolate the source of a message, as there are multiple places that produce the same warning or error message.GAWK_LOCALE_DIRSpecifies the location of compiled message object files for
gawkitself. This is passed to thebindtextdomain()function whengawkstarts up.GAWK_NO_DFAIf this variable exists,
gawkdoes not use the DFA regexp matcher for “does it match” kinds of tests. This can causegawkto be slower. Its purpose is to help isolate differences between the two regexp matchers thatgawkuses internally. (There aren’t supposed to be differences, but occasionally theory and practice don’t coordinate with each other.)GAWK_STACKSIZEThis specifies the amount by which
gawkshould grow its internal evaluation stack, when needed.INT_CHAIN_MAXThis specifies intended maximum number of items
gawkwill maintain on a hash chain for managing arrays indexed by integers.STR_CHAIN_MAXThis specifies intended maximum number of items
gawkwill maintain on a hash chain for managing arrays indexed by strings.TIDYMEMIf this variable exists,
gawkuses themtrace()library calls from the GNU C library to help track down possible memory leaks. This cannot be used together with the persistent memory allocator.
Next: Including Other Files into Your Program, Previous: The Environment Variables gawk Uses, Up: Running awk and gawk [Contents][Index]
2.6 gawk’s Exit Status ¶
If the exit statement is used with a value
(see The exit Statement), then gawk exits with
the numeric value given to it.
Otherwise, if there were no problems during execution,
gawk exits with the value of the C constant
EXIT_SUCCESS. This is usually zero.
If an error occurs, gawk exits with the value of
the C constant EXIT_FAILURE. This is usually one.
If gawk exits because of a fatal error, the exit
status is two. On non-POSIX systems, this value may be mapped
to EXIT_FAILURE.
Next: Loading Dynamic Extensions into Your Program, Previous: gawk’s Exit Status, Up: Running awk and gawk [Contents][Index]
2.7 Including Other Files into Your Program ¶
This section describes a feature that is specific to gawk.
It discusses how one source file may include another.
The @include directive can be used to read external awk source
files. This gives you the ability to split large awk source files
into smaller, more manageable pieces, and also lets you reuse common awk
code from various awk scripts. In other words, you can group
together awk functions used to carry out specific tasks
into external files. These files can be used just like function libraries,
using the @include directive in conjunction with the AWKPATH
environment variable. Note that source files may also be included
using the -i option.
Let’s see an example.
We’ll start with two (trivial) awk scripts, namely
test1 and test2. Here is the test1 script:
BEGIN {
print "This is script test1."
}
and here is test2:
@include "test1"
BEGIN {
print "This is script test2."
}
Running gawk with test2
produces the following result:
$ gawk -f test2 -| This is script test1. -| This is script test2.
gawk runs the test2 script, which includes test1
using the @include
directive. So, to include external awk source files, you just
use @include followed by the name of the file to be included,
enclosed in double quotes.
NOTE: Keep in mind that this is a language construct and the file name cannot be a string variable, but rather just a literal string constant in double quotes.
The files to be included may be nested; e.g., given a third script, namely test3:
@include "test2"
BEGIN {
print "This is script test3."
}
Running gawk with the test3 script produces the
following results:
$ gawk -f test3 -| This is script test1. -| This is script test2. -| This is script test3.
The file name can, of course, be a pathname. For example:
@include "../io_funcs"
and:
@include "/usr/awklib/network"
are both valid. The AWKPATH environment variable can be of great
value when using @include. The same rules for the use
of the AWKPATH variable in command-line file searches
(see The AWKPATH Environment Variable) apply to
@include also.
This is very helpful in constructing gawk function libraries.
If you have a large script with useful, general-purpose awk
functions, you can break it down into library files and put those files
in a special directory. You can then include those “libraries,”
either by using the full pathnames of the files, or by setting the AWKPATH
environment variable accordingly and then using @include with
just the file part of the full pathname. Of course,
you can keep library files in more than one directory;
the more complex the working
environment is, the more directories you may need to organize the files
to be included.
Given the ability to specify multiple -f options, the
@include mechanism is not strictly necessary.
However, the @include directive
can help you in constructing self-contained gawk programs,
thus reducing the need for writing complex and tedious command lines.
In particular, @include is very useful for writing CGI scripts
to be run from web pages.
The @include directive and the -i/--include
command line option are completely equivalent. An included program
source is not loaded if it has been previously loaded.
The rules for finding a source file described in The AWKPATH Environment Variable also
apply to files loaded with @include.
Finally, files included with @include
are treated as if they had ‘@namespace "awk"’
at their beginning. See Changing The Namespace, for more information.
However, there is a separate @nsinclude command which
does not change the namespace; see Including A File Without Changing The Namespace.
Next: Undocumented Options and Features, Previous: Loading Dynamic Extensions into Your Program, Up: Running awk and gawk [Contents][Index]
2.9 Obsolete Options and/or Features ¶
This section describes features and/or command-line options from
previous releases of gawk that either are not available in the
current version or are still supported but deprecated (meaning that
they will not be in a future release).
As of
gawk version 5.2,
the arbitrary precision arithmetic feature is “on parole.”
This feature is now being
supported by a volunteer in the development team and not by the primary
maintainer. If this situation changes, then the feature will be removed.
For more information see Arbitrary Precision Arithmetic is On Parole!.
Next: Summary, Previous: Obsolete Options and/or Features, Up: Running awk and gawk [Contents][Index]
2.10 Undocumented Options and Features ¶
Use the Source, Luke!
— Obi-Wan
This section intentionally left blank.
Previous: Undocumented Options and Features, Up: Running awk and gawk [Contents][Index]
2.11 Summary ¶
gawkparses arguments on the command line, left to right, to determine if they should be treated as options or as non-option arguments.gawkrecognizes several options which control its operation, as described in Command-Line Options. All options begin with ‘-’.- Any argument that is not recognized as an option is treated as a
non-option argument, even if it begins with ‘-’.
- However, when an option itself requires an argument, and the option is separated from that argument on the command line by at least one space, the space is ignored, and the argument is considered to be related to the option. Thus, in the invocation, ‘gawk -F x’, the ‘x’ is treated as belonging to the -F option, not as a separate non-option argument.
- Once
gawkfinds a non-option argument, it stops looking for options. Therefore, all following arguments are also non-option arguments, even if they resemble recognized options. - If no -e or -f options are present,
gawkexpects the program text to be in the first non-option argument. - All non-option arguments, except program text provided in the first
non-option argument, are placed in
ARGVas explained in UsingARGCandARGV, and are processed as described in Other Command-Line Arguments. AdjustingARGCandARGVaffects howawkprocesses input. - The three standard options for all versions of
awkare -f, -F, and -v.gawksupplies these and many others, as well as corresponding GNU-style long options. - Nonoption command-line arguments are usually treated as file names, unless they have the form ‘var=value’, in which case they are taken as variable assignments to be performed at that point in processing the input.
- You can use a single minus sign (‘-’) to refer to standard input
on the command line.
gawkalso lets you use the special file name /dev/stdin. -
gawkpays attention to a number of environment variables.AWKPATH,AWKLIBPATH, andPOSIXLY_CORRECTare the most important ones. gawk’s exit status conveys information to the program that invoked it. Use theexitstatement from within anawkprogram to set the exit status.gawkallows you to include otherawksource files into your program using the@includedirective and/or the -i and -f command-line options.gawkallows you to load additional functions written in C or C++ using the@loaddirective and/or the -l option. (This advanced feature is described later, in Writing Extensions forgawk.)
Next: Reading Input Files, Previous: Running awk and gawk, Up: General Introduction [Contents][Index]
3 Regular Expressions ¶
A regular expression, or regexp, is a way of describing a
set of strings.
Because regular expressions are such a fundamental part of awk
programming, their format and use deserve a separate chapter.
A regular expression enclosed in slashes (‘/’)
is an awk pattern that matches every input record whose text
belongs to that set.
The simplest regular expression is a sequence of letters, numbers, or
both. Such a regexp matches any string that contains that sequence.
Thus, the regexp ‘foo’ matches any string containing ‘foo’.
Thus, the pattern /foo/ matches any input record containing
the three adjacent characters ‘foo’ anywhere in the record. Other
kinds of regexps let you specify more complicated classes of strings.
Initially, the examples in this chapter are simple. As we explain more about how regular expressions work, we present more complicated instances.
- How to Use Regular Expressions
- Escape Sequences
- Regular Expression Operators
- Using Bracket Expressions
- How Much Text Matches?
- Using Dynamic Regexps
gawk-Specific Regexp Operators- Case Sensitivity in Matching
- Selecting the Regexp Matching Engine
- Summary
Next: Escape Sequences, Up: Regular Expressions [Contents][Index]
3.1 How to Use Regular Expressions ¶
A regular expression can be used as a pattern by enclosing it in slashes. Then the regular expression is tested against the entire text of each record. (Normally, it only needs to match some part of the text in order to succeed.) For example, the following prints the second field of each record where the string ‘li’ appears anywhere in the record:
$ awk '/li/ { print $2 }' mail-list
-| 555-5553
-| 555-0542
-| 555-6699
-| 555-3430
Regular expressions can also be used in matching expressions. These
expressions allow you to specify the string to match against; it need
not be the entire current input record. The two operators ‘~’
and ‘!~’ perform regular expression comparisons. Expressions
using these operators can be used as patterns, or in if,
while, for, and do statements.
(See Control Statements in Actions.)
For example, the following is true if the expression exp (taken
as a string) matches regexp:
exp ~ /regexp/
This example matches, or selects, all input records with the uppercase letter ‘J’ somewhere in the first field:
$ awk '$1 ~ /J/' inventory-shipped -| Jan 13 25 15 115 -| Jun 31 42 75 492 -| Jul 24 34 67 436 -| Jan 21 36 64 620
So does this:
awk '{ if ($1 ~ /J/) print }' inventory-shipped
This next example is true if the expression exp (taken as a character string) does not match regexp:
exp !~ /regexp/
The following example matches, or selects, all input records whose first field does not contain the uppercase letter ‘J’:
$ awk '$1 !~ /J/' inventory-shipped -| Feb 15 32 24 226 -| Mar 15 24 34 228 -| Apr 31 52 63 420 -| May 16 34 29 208 ...
When a regexp is enclosed in slashes, such as /foo/, we call it
a regexp constant, much like 5.27 is a numeric constant and
"foo" is a string constant.
Next: Regular Expression Operators, Previous: How to Use Regular Expressions, Up: Regular Expressions [Contents][Index]
3.2 Escape Sequences ¶
Some characters cannot be included literally in string constants
("foo") or regexp constants (/foo/).
Instead, they should be represented with escape sequences,
which are character sequences beginning with a backslash (‘\’).
One use of an escape sequence is to include a double quote character in
a string constant. Because a plain double quote ends the string, you
must use ‘\"’ to represent an actual double quote character as a
part of the string. For example:
$ awk 'BEGIN { print "He said \"hi!\" to her." }'
-| He said "hi!" to her.
The backslash character itself is another character that cannot be
included normally; you must write ‘\\’ to put one backslash in the
string or regexp. Thus, the string whose contents are the two characters
‘"’ and ‘\’ must be written "\"\\".
Other escape sequences represent unprintable characters such as TAB or newline. There is nothing to stop you from entering most unprintable characters directly in a string constant or regexp constant, but they may look ugly.
The following list presents
all the escape sequences used in awk and
what they represent. Unless noted otherwise, all these escape
sequences apply to both string constants and regexp constants:
\\A literal backslash, ‘\’.
\aThe “alert” character, Ctrl-g, ASCII code 7 (BEL). (This often makes some sort of audible noise.)
\bBackspace, Ctrl-h, ASCII code 8 (BS).
\fFormfeed, Ctrl-l, ASCII code 12 (FF).
\nNewline, Ctrl-j, ASCII code 10 (LF).
\rCarriage return, Ctrl-m, ASCII code 13 (CR).
\tHorizontal TAB, Ctrl-i, ASCII code 9 (HT).
\vVertical TAB, Ctrl-k, ASCII code 11 (VT).
\nnnThe octal value nnn, where nnn stands for 1 to 3 digits between ‘0’ and ‘7’. For example, the code for the ASCII ESC (escape) character is ‘\033’.
\xhh…The hexadecimal value hh, where hh stands for a sequence of hexadecimal digits (‘0’–‘9’, and either ‘A’–‘F’ or ‘a’–‘f’). A maximum of two digits are allowed after the ‘\x’. Any further hexadecimal digits are treated as simple letters or numbers. (c.e.) (The ‘\x’ escape sequence is not allowed in POSIX awk.)
CAUTION: In ISO C, the escape sequence continues until the first nonhexadecimal digit is seen. For many years,
gawkwould continue incorporating hexadecimal digits into the value until a non-hexadecimal digit or the end of the string was encountered. However, using more than two hexadecimal digits produced undefined results. As of version 4.2, only two digits are processed.\uhh…The hexadecimal value hh, where hh stands for a sequence of one or more hexadecimal digits (‘0’–‘9’, and either ‘A’–‘F’ or ‘a’–‘f’). A maximum of eight digits are allowed after the ‘\u’. Any further hexadecimal digits are treated as simple letters or numbers. (c.e.) (The ‘\u’ escape sequence is not allowed in POSIX awk.)
This escape sequence is intended for designating a character in the current locale’s character set.16
gawkfirst converts the given digits into an integer and then translates the given “wide character” value into the current locale’s multibyte encoding. If the wide character value does not represent a valid character, or if the character is valid but cannot be encoded into the current locale’s multibyte encoding, the value becomes"?".gawkissues a warning message when this happens.\/A literal slash (should be used for regexp constants only). This sequence is used when you want to write a regexp constant that contains a slash (such as
/.*:\/home\/[[:alnum:]]+:.*/; the ‘[[:alnum:]]’ notation is discussed in Using Bracket Expressions). Because the regexp is delimited by slashes, you need to escape any slash that is part of the pattern, in order to tellawkto keep processing the rest of the regexp.\"A literal double quote (should be used for string constants only). This sequence is used when you want to write a string constant that contains a double quote (such as
"He said \"hi!\" to her."). Because the string is delimited by double quotes, you need to escape any double quote that is part of the string, in order to tellawkto keep processing the rest of the string.
In gawk, a number of additional two-character sequences that begin
with a backslash have special meaning in regexps.
See gawk-Specific Regexp Operators.
In a regexp, a backslash before any character that is not in the previous list
and not listed in
gawk-Specific Regexp Operators
means that the next character should be taken literally, even if it would
normally be a regexp operator. For example, /a\+b/ matches the three
characters ‘a+b’.
For complete portability, do not use a backslash before any character not shown in the previous list or that is not a regular expression operator. (The 2024 POSIX standard explicitly lists the operators that can be escaped, leaving it undefined as to what happens for any other escaped character. But the bottom line is as described previously.)
| Backslash Before Regular Characters |
|---|
|
If you place a backslash in a string constant before something that is
not one of the characters previously listed, POSIX
|
To summarize:
- The escape sequences in the preceding list are always processed first,
for both string constants and regexp constants. This happens very early,
as soon as
awkreads your program. gawkprocesses both regexp constants and dynamic regexps (see Using Dynamic Regexps), for the special operators listed ingawk-Specific Regexp Operators.- A backslash before any other character means to treat that character literally.
| Escape Sequences for Metacharacters |
|---|
|
Suppose you use an octal or hexadecimal ( Historically, such characters were taken literally.
(d.c.)
However, the POSIX standard indicates that they should be treated
as real metacharacters, which is what |
Next: Using Bracket Expressions, Previous: Escape Sequences, Up: Regular Expressions [Contents][Index]
3.3 Regular Expression Operators ¶
You can combine regular expressions with special characters, called regular expression operators or metacharacters, to increase the power and versatility of regular expressions.
- Regexp Operators in
awk - Some Notes On Interval Expressions
- Shortest Match, or “Non-greedy” Regexp Operators
Next: Some Notes On Interval Expressions, Up: Regular Expression Operators [Contents][Index]
3.3.1 Regexp Operators in awk ¶
The escape sequences described earlier in Escape Sequences are valid inside a regexp. They are introduced by a ‘\’ and are recognized and converted into corresponding real characters as the very first step in processing regexps.
Here is a list of metacharacters. All characters that are not escape sequences and that are not listed here stand for themselves:
\This suppresses the special meaning of a character when matching. For example, ‘\$’ matches the character ‘$’.
^This matches the beginning of a string. ‘^@chapter’ matches ‘@chapter’ at the beginning of a string, for example, and can be used to identify chapter beginnings in Texinfo source files. The ‘^’ is known as an anchor, because it anchors the pattern to match only at the beginning of the string.
It is important to realize that ‘^’ does not match the beginning of a line (the point right after a ‘\n’ newline character) embedded in a string. The condition is not true in the following example:
if ("line1\nLINE 2" ~ /^L/) ...$This is similar to ‘^’, but it matches only at the end of a string. For example, ‘p$’ matches a record that ends with a ‘p’. The ‘$’ is an anchor and does not match the end of a line (the point right before a ‘\n’ newline character) embedded in a string. The condition in the following example is not true:
if ("line1\nLINE 2" ~ /1$/) ...-
.(period) ¶ This matches any single character, including the newline character. For example, ‘.P’ matches any single character followed by a ‘P’ in a string. Using concatenation, we can make a regular expression such as ‘U.A’, which matches any three-character sequence that begins with ‘U’ and ends with ‘A’.
In strict POSIX mode (see Command-Line Options), ‘.’ does not match the NUL character, which is a character with all bits equal to zero. Otherwise, NUL is just another character. Other versions of
awkmay not be able to match the NUL character.-
[…]¶ This is called a bracket expression.17 It matches any one of the characters that are enclosed in the square brackets. For example, ‘[MVX]’ matches any one of the characters ‘M’, ‘V’, or ‘X’ in a string. A full discussion of what can be inside the square brackets of a bracket expression is given in Using Bracket Expressions.
[^…]This is a complemented bracket expression. The first character after the ‘[’ must be a ‘^’. It matches any characters except those in the square brackets. For example, ‘[^awk]’ matches any character that is not an ‘a’, ‘w’, or ‘k’.
-
|¶ This is the alternation operator and it is used to specify alternatives. The ‘|’ has the lowest precedence of all the regular expression operators. For example, ‘^P|[aeiouy]’ matches any string that matches either ‘^P’ or ‘[aeiouy]’. This means it matches any string that starts with ‘P’ or contains (anywhere within it) a lowercase English vowel.
The alternation applies to the largest possible regexps on either side.
(…)Parentheses are used for grouping in regular expressions, as in arithmetic. They can be used to concatenate regular expressions containing the alternation operator, ‘|’. For example, ‘@(samp|code)\{[^}]+\}’ matches both ‘@code{foo}’ and ‘@samp{bar}’. (These are Texinfo formatting control sequences. The ‘+’ is explained further on in this list.)
The left or opening parenthesis is always a metacharacter; to match one literally, precede it with a backslash. However, the right or closing parenthesis is only special when paired with a left parenthesis; an unpaired right parenthesis is (silently) treated as a regular character.
*This symbol means that the preceding regular expression should be repeated as many times as necessary to find a match. For example, ‘ph*’ applies the ‘*’ symbol to the preceding ‘h’ and looks for matches of one ‘p’ followed by any number of ‘h’s. This also matches just ‘p’ if no ‘h’s are present.
There are two subtle points to understand about how ‘*’ works. First, the ‘*’ applies only to the single preceding regular expression component (e.g., in ‘ph*’, it applies just to the ‘h’). To cause ‘*’ to apply to a larger subexpression, use parentheses: ‘(ph)*’ matches ‘ph’, ‘phph’, ‘phphph’, and so on.
Second, ‘*’ finds as many repetitions as possible. If the text to be matched is ‘phhhhhhhhhhhhhhooey’, ‘ph*’ matches all of the ‘h’s.
*?Shortest match or non-greedy version of the ‘*’ operator. This operator and the other non-greedy operators are discussed separately, shortly, in Shortest Match, or “Non-greedy” Regexp Operators.
+This symbol is similar to ‘*’, except that the preceding expression must be matched at least once. This means that ‘wh+y’ would match ‘why’ and ‘whhy’, but not ‘wy’, whereas ‘wh*y’ would match all three.
+?Shortest match or non-greedy version of the ‘+’ operator.
?This symbol is similar to ‘*’, except that the preceding expression can be matched either once or not at all. For example, ‘fe?d’ matches ‘fed’ and ‘fd’, but nothing else.
??Shortest match or non-greedy version of the ‘?’ operator.
{n}¶{n,}{n,m}One or two numbers inside braces denote an interval expression. If there is one number in the braces, the preceding regexp is repeated n times. If there are two numbers separated by a comma, the preceding regexp is repeated n to m times. If there is one number followed by a comma, then the preceding regexp is repeated at least n times:
wh{3}yMatches ‘whhhy’, but not ‘why’ or ‘whhhhy’.
wh{3,5}yMatches ‘whhhy’, ‘whhhhy’, or ‘whhhhhy’ only.
wh{2,}yMatches ‘whhy’, ‘whhhy’, and so on.
{n}?{n,}?{n,m}?Shortest match or non-greedy versions of the ‘{}’ operators.
In regular expressions, the ‘*’, ‘+’, and ‘?’ operators, as well as the braces ‘{’ and ‘}’, have the highest precedence, followed by concatenation, and finally by ‘|’. As in arithmetic, parentheses can change how operators are grouped.
According to the POSIX specification, when ‘*’, ‘+’, ‘?’,
or ‘{’ are not preceded by a character, the behavior is
“undefined.”
In practice, for gawk, the ‘*’, ‘+’, ‘?’ and
‘{’ operators stand for themselves when there is nothing in the
regexp that precedes them. For example, /+/ matches a literal
plus sign. However, many other versions of awk treat such a
usage as a syntax error.
| What About The Empty Regexp? |
|---|
|
We describe here an advanced regexp usage. Feel free to skip it upon first reading. You can supply an empty regexp constant (‘//’) in all places where a regexp is expected. Is this useful? What does it match? It is useful. It matches the (invisible) empty string at the start
and end of a string of characters, as well as the empty string
between characters. This is best illustrated with the $ awk '
> BEGIN {
> x = "ABC_CBA"
> gsub(/B/, "bb", x)
> print x
> }'
-| AbbC_CbbA
We can use $ awk '
> BEGIN {
> x = "ABC"
> gsub(//, "x", x)
> print x
> }'
-| xAxBxCx
|
Next: Shortest Match, or “Non-greedy” Regexp Operators, Previous: Regexp Operators in awk, Up: Regular Expression Operators [Contents][Index]
3.3.2 Some Notes On Interval Expressions ¶
Interval expressions were not traditionally available in awk.
They were added as part of the POSIX standard to make awk
and egrep consistent with each other.
Initially, because old programs may use ‘{’ and ‘}’ in regexp
constants,
gawk did not match interval expressions
in regexps.
However, beginning with version 4.0,
gawk does match interval expressions by default.
This is because compatibility with POSIX has become more
important to most gawk users than compatibility with
old programs.
For programs that use ‘{’ and ‘}’ in regexp constants,
it is good practice to always escape them with a backslash. Then the
regexp constants are valid and work the way you want them to, using
any version of awk.18
When ‘{’ and ‘}’ appear in regexp constants
in a way that cannot be interpreted as an interval expression
(such as /q{a}/), then they stand for themselves.
As mentioned, interval expressions were not traditionally available
in awk. In March of 2019, BWK awk (finally) acquired them.
Starting with version 5.2, gawk’s
--traditional option no longer disables interval
expressions in regular expressions.
POSIX says that interval expressions containing repetition counts greater than 255 produce unspecified results.
In the manual for GNU grep, Paul Eggert notes the following:
Interval expressions may be implemented internally via repetition. For example, ‘^(a|bc){2,4}$’ might be implemented as ‘^(a|bc)(a|bc)((a|bc)(a|bc)?)?$’. A large repetition count may exhaust memory or greatly slow matching. Even small counts can cause problems if cascaded; for example, ‘grep -E ".*{10,}{10,}{10,}{10,}{10,}"’ is likely to overflow a stack. Fortunately, regular expressions like these are typically artificial, and cascaded repetitions do not conform to POSIX so cannot be used in portable programs anyway.
This same caveat applies to gawk.
Previous: Some Notes On Interval Expressions, Up: Regular Expression Operators [Contents][Index]
3.3.3 Shortest Match, or “Non-greedy” Regexp Operators ¶
Traditionally, regexps have always matched the leftmost, longest sequence of characters. For example, given the text ‘abxxxcd’, the regexp ‘x+’ could match one, two, or all three ‘x’ characters. By defining a regexp to match the longest possible sequence, we know that ‘x+’ will match all three ‘x’s. Such behavior may be termed greedy, since a regexp will match as many characters as possible.
The Perl language introduced “non-greedy,” or shortest-match operators. In the above example, ‘x+’ would only match a single ‘x’.
The 2024 POSIX standard introduced shortest-match operators into POSIX EREs. Syntactically, you create a shortest-match operator by appending a ‘?’ to one of the traditional regexp operators: ‘*’, ‘+’, ‘?’, or ‘{}’.
The shortest-match operators don’t always match what you might think would be the smallest sequence of characters, since the general, leftmost-longest rule still applies to the overall regexp.
To illustrate the differences, we use a program named shortest-match.awk.
This program takes several strings, and replaces the text matching a given
pattern with a single ‘X’. The program uses a number of gawk-specific
features which haven’t yet been described. We therefore delay presenting the
code until Demonstrating Shortest and Longest Match Operators.
For each string, the program prints the following:
- The original string, in quotes.
- The pattern used with a shortest match operator and the result, indented.
- Further indented is a list of the start and lengths of the submatches. Submatch zero is the whole string, submatch one is the first parenthesized expression, and so on.
- The pattern used with a regular match operator and the result, indented.
- The same start plus length information for submatches of the long pattern.
Here is the output, with commentary:
"aaaxxxzzz"
shortpat: /x+?/, result: "aaaXxxzzz"
0: (s: 4, l: 1) -> "x"
longpat: /x+/, result: "aaaXzzz"
0: (s: 4, l: 3) -> "xxx"
For the shortest match, only one ‘x’ is replaced. For the longest match, every ‘x’ is replaced. This is pretty straightforward.
"aaaxxxyzzz"
shortpat: /x+?y/, result: "aaaXzzz"
0: (s: 4, l: 4) -> "xxxy"
longpat: /x+y/, result: "aaaXzzz"
0: (s: 4, l: 4) -> "xxxy"
In this case, the result is the same, since the longest possible string of characters is matched.
"aaaxxxxxxxxxxxxxxxxzzz"
shortpat: /(x+?)(x+)(x+?)(x+)/, result: "aaaXzzz"
0: (s: 4, l: 16) -> "xxxxxxxxxxxxxxxx"
1: (s: 4, l: 1) -> "x"
2: (s: 5, l: 13) -> "xxxxxxxxxxxxx"
3: (s: 18, l: 1) -> "x"
4: (s: 19, l: 1) -> "x"
longpat: /(x+)(x+)(x+)(x+)/, result: "aaaXzzz"
0: (s: 4, l: 16) -> "xxxxxxxxxxxxxxxx"
1: (s: 4, l: 13) -> "xxxxxxxxxxxxx"
2: (s: 17, l: 1) -> "x"
3: (s: 18, l: 1) -> "x"
4: (s: 19, l: 1) -> "x"
This case is more interesting. The same total amount of text is matched for both patterns. However, the text matched by the subexpressions differs between the two cases. In the longest-match case, the longest possible subexpressions match first.
"aaaxyxxyxxyxzzz"
shortpat: /((x+)(y+?)(x+))+/, result: "aaaXyxxyxzzz"
0: (s: 4, l: 4) -> "xyxx"
1: (s: 4, l: 4) -> "xyxx"
2: (s: 4, l: 1) -> "x"
3: (s: 5, l: 1) -> "y"
4: (s: 6, l: 2) -> "xx"
longpat: /((x+)(y+)(x+))+/, result: "aaaXzzz"
0: (s: 4, l: 9) -> "xyxxyxxyx"
1: (s: 10, l: 3) -> "xyx"
2: (s: 10, l: 1) -> "x"
3: (s: 11, l: 1) -> "y"
4: (s: 12, l: 1) -> "x"
This case needs careful examination. There is a ‘+’ on the outside, meaning that the total inner expression may match multiple times. For the longest-match expression, that indeed happens. The subexpressions two through four have the start and length values for the last occasion where the inner expression matched. The shortest-match expression only matched the inner expression once.
"aaaxyxxyxxyxzzz"
shortpat: /((x+)(y+?)(x+)){2}/, result: "aaaXyxzzz"
0: (s: 4, l: 7) -> "xyxxyxx"
1: (s: 7, l: 4) -> "xyxx"
2: (s: 7, l: 1) -> "x"
3: (s: 8, l: 1) -> "y"
4: (s: 9, l: 2) -> "xx"
longpat: /((x+)(y+)(x+)){2}/, result: "aaaXyxzzz"
0: (s: 4, l: 7) -> "xyxxyxx"
1: (s: 7, l: 4) -> "xyxx"
2: (s: 7, l: 1) -> "x"
3: (s: 8, l: 1) -> "y"
4: (s: 9, l: 2) -> "xx"
Here, the results are identical; both the nature of the string to be matched and the POSIX longest-leftmost rule, cause this to happen.
The MinRX regular expression matching engine, which is gawk’s
default matcher (see Selecting the Regexp Matching Engine), supports the shortest-match
operators. The older GNU matchers do not. Also, when invoked with the
--traditional option, shortest-match operators are not supported,
since traditional awk does not have these operators.
Next: How Much Text Matches?, Previous: Regular Expression Operators, Up: Regular Expressions [Contents][Index]
3.4 Using Bracket Expressions ¶
As mentioned earlier, a bracket expression matches any character among those listed between the opening and closing square brackets.
Within a bracket expression, a range expression consists of two
characters separated by a hyphen. It matches any single character that
sorts between the two characters, based upon the system’s native character
set. For example, ‘[0-9]’ is equivalent to ‘[0123456789]’.
(See Regexp Ranges and Locales: A Long Sad Story for an explanation of how the POSIX
standard and gawk have changed over time. This is mainly
of historical interest.)
With the increasing popularity of the
Unicode character standard,
there is an additional wrinkle to consider. Octal and hexadecimal
escape sequences inside bracket expressions are taken to represent
only single-byte characters (characters whose values fit within
the range 0–255). To match a range of characters where the endpoints
of the range are larger than 255, enter the multibyte encodings of
the characters directly, or use the \u escape sequence.
To include one of the characters ‘\’, ‘]’, ‘-’, or ‘^’ in a bracket expression, put a ‘\’ in front of it. For example:
[d\]]
matches either ‘d’ or ‘]’. Additionally, if you place ‘]’ right after the opening ‘[’ (‘[]d]’), the closing bracket is treated as one of the characters to be matched. Inside bracket expressions, it’s not necessary to escape the other standard regular expression operators, such as ‘*’ and ‘?’, and for full portability you should not.
NOTE: Note that the additional regular expression operators that begin with a backslash, such as ‘\<’, or ‘\w’, have no meaning when used inside a bracket expression. There, the backslash is taken to mean escape the following character, so ‘[\w]’ is the same as ‘[w]’, and ‘[\<]’ is the same as ‘[<]’.
The treatment of ‘\’ in bracket expressions
is compatible with other awk
implementations and is also mandated by POSIX.19
The regular expressions in awk are a superset
of the POSIX specification for Extended Regular Expressions (EREs).
POSIX EREs are based on the regular expressions accepted by the
traditional egrep utility.
Character classes are a feature introduced in the POSIX standard. A character class is a special notation for describing lists of characters that have a specific attribute, but the actual characters can vary from country to country and/or from character set to character set. For example, the notion of what is an alphabetic character differs between the United States and France.
A character class is only valid in a regexp inside the brackets of a bracket expression. Character classes consist of ‘[:’, a keyword denoting the class, and ‘:]’. Table 3.1 lists the character classes defined by the POSIX standard.
| Class | Meaning |
|---|---|
[:alnum:] | Alphanumeric characters |
[:alpha:] | Alphabetic characters |
[:blank:] | Space and TAB characters |
[:cntrl:] | Control characters |
[:digit:] | Numeric characters |
[:graph:] | Characters that are both printable and visible (a space is printable but not visible, whereas an ‘a’ is both) |
[:lower:] | Lowercase alphabetic characters |
[:print:] | Printable characters (characters that are not control characters) |
[:punct:] | Punctuation characters (characters that are not letters, digits, control characters, or space characters) |
[:space:] | Space characters (these are: space, TAB, newline, carriage return, formfeed and vertical tab) |
[:upper:] | Uppercase alphabetic characters |
[:xdigit:] | Characters that are hexadecimal digits |
Table 3.1: POSIX character classes
For example, before the POSIX standard, you had to write /[A-Za-z0-9]/
to match alphanumeric characters. If your
character set had other alphabetic characters in it, this would not
match them.
With the POSIX character classes, you can write
/[[:alnum:]]/ to match the alphabetic
and numeric characters in your character set.
Some utilities that match regular expressions provide a nonstandard
‘[:ascii:]’ character class; awk does not. However, you
can simulate such a construct using ‘[\x00-\x7F]’. This matches
all values numerically between zero and 127, which is the defined
range of the ASCII character set. Use a complemented character list
(‘[^\x00-\x7F]’) to match any single-byte characters that are not
in the ASCII range.
NOTE: Some older versions of Unix
awktreat[:blank:]like[:space:], incorrectly matching more characters than they should. Caveat Emptor.
Two additional special sequences can appear in bracket expressions. These apply to non-ASCII character sets, which can have single symbols (called collating elements) that are represented with more than one character. They can also have several characters that are equivalent for collating, or sorting, purposes. (For example, in French, a plain “e” and a grave-accented “è” are equivalent.) These sequences are:
- Collating symbols ¶
Multicharacter collating elements enclosed between ‘[.’ and ‘.]’. For example, if ‘ch’ is a collating element, then ‘[[.ch.]]’ is a regexp that matches this collating element, whereas ‘[ch]’ is a regexp that matches either ‘c’ or ‘h’.
- Equivalence classes
Locale-specific names for a list of characters that are equal. The name is enclosed between ‘[=’ and ‘=]’. For example, the name ‘e’ might be used to represent all of “e,” “ê,” “è,” and “é.” In this case, ‘[[=e=]]’ is a regexp that matches any of ‘e’, ‘ê’, ‘é’, or ‘è’.
These features are very valuable in non-English-speaking locales.
CAUTION: The library functions that
gawkuses for regular expression matching currently recognize only POSIX character classes; they do not recognize collating symbols or equivalence classes.
Inside a bracket expression, an opening bracket (‘[’) that does not start a character class, collating element or equivalence class is taken literally. This is also true of ‘.’ and ‘*’.
Next: Using Dynamic Regexps, Previous: Using Bracket Expressions, Up: Regular Expressions [Contents][Index]
3.5 How Much Text Matches? ¶
Consider the following:
echo aaaabcd | awk '{ sub(/a+/, "<A>"); print }'
This example uses the sub() function to make a change to the input
record. (sub() replaces the first instance of any text matched
by the first argument with the string provided as the second argument;
see String-Manipulation Functions.) Here, the regexp /a+/ indicates “one
or more ‘a’ characters,” and the replacement text is ‘<A>’.
The input contains four ‘a’ characters.
awk (and POSIX) regular expressions always match
the leftmost, longest sequence of input characters that can
match. Thus, all four ‘a’ characters are
replaced with ‘<A>’ in this example:
$ echo aaaabcd | awk '{ sub(/a+/, "<A>"); print }'
-| <A>bcd
For simple match/no-match tests, this is not so important. But when doing
text matching and substitutions with the match(), sub(), gsub(),
and gensub() functions, it is very important.
Understanding this principle is also important for regexp-based record
and field splitting (see How Input Is Split into Records,
and also see Specifying How Fields Are Separated).
Next: gawk-Specific Regexp Operators, Previous: How Much Text Matches?, Up: Regular Expressions [Contents][Index]
3.6 Using Dynamic Regexps ¶
The righthand side of a ‘~’ or ‘!~’ operator need not be a regexp constant (i.e., a string of characters between slashes). It may be any expression. The expression is evaluated and converted to a string if necessary; the contents of the string are then used as the regexp. A regexp computed in this way is called a dynamic regexp or a computed regexp:
BEGIN { digits_regexp = "[[:digit:]]+" }
$0 ~ digits_regexp { print }
This sets digits_regexp to a regexp that describes one or more digits,
and tests whether the input record matches this regexp.
NOTE: When using the ‘~’ and ‘!~’ operators, be aware that there is a difference between a regexp constant enclosed in slashes and a string constant enclosed in double quotes. If you are going to use a string constant, you have to understand that the string is, in essence, scanned twice: the first time when
awkreads your program, and the second time when it goes to match the string on the lefthand side of the operator with the pattern on the right. This is true of any string-valued expression (such asdigits_regexp, shown in the previous example), not just string constants.
What difference does it make if the string is scanned twice? The answer has to do with escape sequences, and particularly with backslashes. To get a backslash into a regular expression inside a string, you have to type two backslashes.
For example, /\*/ is a regexp constant for a literal ‘*’.
Only one backslash is needed. To do the same thing with a string,
you have to type "\\*". The first backslash escapes the
second one so that the string actually contains the
two characters ‘\’ and ‘*’.
Given that you can use both regexp and string constants to describe regular expressions, which should you use? The answer is “regexp constants,” for several reasons:
- String constants are more complicated to write and more difficult to read. Using regexp constants makes your programs less error-prone. Not understanding the difference between the two kinds of constants is a common source of errors.
- It is more efficient to use regexp constants.
awkcan note that you have supplied a regexp and store it internally in a form that makes pattern matching more efficient. When using a string constant,awkmust first convert the string into this internal form and then perform the pattern matching. - Using regexp constants is better form; it shows clearly that you intend a regexp match.
Next: Case Sensitivity in Matching, Previous: Using Dynamic Regexps, Up: Regular Expressions [Contents][Index]
3.7 gawk-Specific Regexp Operators ¶
GNU software that deals with regular expressions provides a number of
additional regexp operators. These operators are described in this
section and are specific to gawk;
they are not available in other awk implementations.
Most of the additional operators deal with word matching.
For our purposes, a word is a sequence of one or more letters, digits,
or underscores (‘_’):
\sMatches any space character as defined by the current locale. Think of it as shorthand for ‘[[:space:]]’.
\SMatches any character that is not a space, as defined by the current locale. Think of it as shorthand for ‘[^[:space:]]’.
\wMatches any word-constituent character—that is, it matches any letter, digit, or underscore. Think of it as shorthand for ‘[[:alnum:]_]’.
\WMatches any character that is not word-constituent. Think of it as shorthand for ‘[^[:alnum:]_]’.
\<Matches the empty string at the beginning of a word. For example,
/\<away/matches ‘away’ but not ‘stowaway’.\>Matches the empty string at the end of a word. For example,
/stow\>/matches ‘stow’ but not ‘stowaway’.\y¶Matches the empty string at either the beginning or the end of a word (i.e., the word boundary). For example, ‘\yballs?\y’ matches either ‘ball’ or ‘balls’, as a separate word.
\BMatches the empty string that occurs between two word-constituent characters. For example,
/\Brat\B/matches ‘crate’, but it does not match ‘dirty rat’. ‘\B’ is essentially the opposite of ‘\y’. Another way to think of this is that ‘\B’ matches the empty string provided it’s not at the edge of a word.
There are two other operators that work on buffers. In Emacs, a
buffer is, naturally, an Emacs buffer.
Other GNU programs, including gawk,
consider the entire string to match as the buffer.
The operators are:
\`-
Matches the empty string at the beginning of a buffer (string)
\'Matches the empty string at the end of a buffer (string)
Because ‘^’ and ‘$’ always work in terms of the beginning
and end of strings, these operators don’t add any new capabilities
for awk. They are provided for compatibility with other
GNU software.
In other GNU software, the word-boundary operator is ‘\b’. However,
that conflicts with the awk language’s definition of ‘\b’
as backspace, so gawk uses a different letter.
An alternative method would have been to require two backslashes in the
GNU operators, but this was deemed too confusing. The current
method of using ‘\y’ for the GNU ‘\b’ appears to be the
lesser of two evils.
| Backreferences Are Not Supported |
|---|
|
In POSIX Basic Regular Expressions (BREs), you can specify what are
called backreferences in regular expressions. For instance,
in This construct is not supported in POSIX Extended Regular Expressions
(EREs) such as are used in This is true even though the underlying regexp matching engine(s) used
by We are told that BusyBox |
The various command-line options
(see Command-Line Options)
control how gawk interprets characters in regexps:
- No options
In the default case,
gawkprovides all the facilities of POSIX regexps and the previously described GNU regexp operators. GNU regexp operators described in Regular Expression Operators.- --posix
Match only POSIX regexps; the GNU operators are not special (e.g., ‘\w’ matches a literal ‘w’). Interval expressions are allowed.
- --traditional ¶
Match traditional Unix
awkregexps. The GNU operators are not special. Because BWKawksupports them, the POSIX character classes (‘[[:alnum:]]’, etc.) are available. So too, interval expressions are allowed. Characters described by octal and hexadecimal escape sequences are treated literally, even if they represent regexp metacharacters.- --re-interval
This option remains for backwards compatibility but no longer has any real effect.
Next: Selecting the Regexp Matching Engine, Previous: gawk-Specific Regexp Operators, Up: Regular Expressions [Contents][Index]
3.8 Case Sensitivity in Matching ¶
Case is normally significant in regular expressions, both when matching ordinary characters (i.e., not metacharacters) and inside bracket expressions. Thus, a ‘w’ in a regular expression matches only a lowercase ‘w’ and not an uppercase ‘W’.
The simplest way to do a case-independent match is to use a bracket expression—for example, ‘[Ww]’. However, this can be cumbersome if you need to use it often, and it can make the regular expressions harder to read. There are two alternatives that you might prefer.
One way to perform a case-insensitive match at a particular point in the
program is to convert the data to a single case, using the
tolower() or toupper() built-in string functions (which we
haven’t discussed yet;
see String-Manipulation Functions).
For example:
tolower($1) ~ /foo/ { ... }
converts the first field to lowercase before matching against it.
This works in any POSIX-compliant awk.
Another method, specific to gawk, is to set the variable
IGNORECASE to a nonzero value (see Predefined Variables).
When IGNORECASE is not zero, all regexp and string
operations ignore case.
Changing the value of IGNORECASE dynamically controls the
case sensitivity of the program as it runs. Case is significant by
default because IGNORECASE (like most variables) is initialized
to zero:
x = "aB" if (x ~ /ab/) ... # this test will fail IGNORECASE = 1 if (x ~ /ab/) ... # now it will succeed
In general, you cannot use IGNORECASE to make certain rules
case insensitive and other rules case sensitive, as there is no
straightforward way
to set IGNORECASE just for the pattern of
a particular rule.20
To do this, use either bracket expressions or tolower(). However, one
thing you can do with IGNORECASE only is dynamically turn
case sensitivity on or off for all the rules at once.
IGNORECASE can be set on the command line or in a BEGIN rule
(see Other Command-Line Arguments; also
see Startup and Cleanup Actions).
Setting IGNORECASE from the command line is a way to make
a program case insensitive without having to edit it.
In multibyte locales, the equivalences between upper- and lowercase characters are tested based on the wide-character values of the locale’s character set. Prior to version 5.0, single-byte characters were tested based on the ISO-8859-1 (ISO Latin-1) character set. However, as of version 5.0, single-byte characters are also tested based on the values of the locale’s character set.21
The value of IGNORECASE has no effect if gawk is in
compatibility mode (see Command-Line Options).
Case is always significant in compatibility mode.
Next: Summary, Previous: Case Sensitivity in Matching, Up: Regular Expressions [Contents][Index]
3.9 Selecting the Regexp Matching Engine ¶
Release 5.4.0 of gawk introduced a new
regular expression matching engine, named
MinRX.
MinRX is fully compliant with the POSIX standard for Extended
Regular Expressions (EREs), including the additional features needed
by awk and gawk. It is also a little stricter
that the original matchers are
in terms of accepting valid regular expression syntax when specifying
a regexp. (These restrictions apply to corner cases that should
not come up in day-to-day use.)
Previously, gawk used GNU regex and dfa
from GNULIB. These matchers are fast and generally robust, albeit not
fully POSIX compliant. MinRX replaces both of them.
Because regular expression matching is such a fundamental part of what
awk programs do, introducing a new regular expression engine
has some risk associated with it. To alleviate the risk, for the
term of one major release, gawk continues to provide access
to the original regexp matchers should that be needed.
If the environment variable GAWK_GNU_MATCHERS exists, then
gawk switches to using GNU regex and dfa,
as previously. Otherwise, the MinRX matcher is the default and
that is what it uses.
Should you find a need to switch from MinRX to the original matchers, please submit a bug report describing what did not work (see Reporting Problems and Bugs). Doing so is very important, as it will help the maintainers and the MinRX author fix any issues that are found.
After one major release, the old matchers, and the use of the
GAWK_GNU_MATCHERS environment variable, will be removed
from gawk.
Previous: Selecting the Regexp Matching Engine, Up: Regular Expressions [Contents][Index]
3.10 Summary ¶
- Regular expressions describe sets of strings to be matched.
In
awk, regular expression constants are written enclosed between slashes:/…/. - Regexp constants may be used standalone in patterns and in conditional expressions, or as part of matching expressions using the ‘~’ and ‘!~’ operators.
- Escape sequences let you represent nonprintable characters and also let you represent regexp metacharacters as literal characters to be matched.
- Regexp operators provide grouping, alternation, and repetition.
- Bracket expressions give you a shorthand for specifying sets of characters that can match at a particular point in a regexp. Within bracket expressions, POSIX character classes let you specify certain groups of characters in a locale-independent fashion.
- Regular expressions match the leftmost longest text in the string being matched. This matters for cases where you need to know the extent of the match, such as for text substitution and when the record separator is a regexp.
- Matching expressions may use dynamic regexps (i.e., string values treated as regular expressions).
gawk’sIGNORECASEvariable lets you control the case sensitivity of regexp matching. In otherawkversions, usetolower()ortoupper().
Next: Printing Output, Previous: Regular Expressions, Up: General Introduction [Contents][Index]
4 Reading Input Files ¶
In the typical awk program,
awk reads all input either from the
standard input (by default, this is the keyboard, but often it is a pipe from another
command) or from files whose names you specify on the awk
command line. If you specify input files, awk reads them
in order, processing all the data from one before going on to the next.
The name of the current input file can be found in the predefined variable
FILENAME
(see Predefined Variables).
The input is read in units called records, and is processed by the rules of your program one record at a time. By default, each record is one line. Each record is automatically split into chunks called fields. This makes it more convenient for programs to work on the parts of a record.
On rare occasions, you may need to use the getline function.
The getline function is valuable both because it
can do explicit input from any number of files, and because the files
used with it do not have to be named on the awk command line
(see Explicit Input with getline).
- How Input Is Split into Records
- Examining Fields
- Nonconstant Field Numbers
- Changing the Contents of a Field
- Specifying How Fields Are Separated
- Reading Fixed-Width Data
- Defining Fields by Content
- Checking How
gawkIs Splitting Records - Multiple-Line Records
- Explicit Input with
getline - Reading Input with a Timeout
- Retrying Reads After Certain Input Errors
- Directories on the Command Line
- Summary
- Exercises
Next: Examining Fields, Up: Reading Input Files [Contents][Index]
4.1 How Input Is Split into Records ¶
awk divides the input for your program into records and fields.
It keeps track of the number of records that have been read so far from
the current input file. This value is stored in a predefined variable
called FNR, which is reset to zero every time a new file is started.
Another predefined variable, NR, records the total number of input
records read so far from all data files. It starts at zero, but is
never automatically reset to zero.
Normally, records are separated by newline characters. You can control how
records are separated by assigning values to the built-in variable RS.
If RS is any single character, that character separates records.
Otherwise (in gawk), RS is treated as a regular expression.
This mechanism is explained in greater detail shortly.
NOTE: When
gawkis invoked with the --csv option, nothing in this section applies. See Working With Comma Separated Value Files, for the details.
Next: Record Splitting with gawk, Up: How Input Is Split into Records [Contents][Index]
4.1.1 Record Splitting with Standard awk ¶
Records are separated by a character called the record separator.
By default, the record separator is the newline character.
This is why records are, by default, single lines.
To use a different character for the record separator,
simply assign that character to the predefined variable RS.
Like any other variable,
the value of RS can be changed in the awk program
with the assignment operator, ‘=’
(see Assignment Expressions).
The new record-separator character should be enclosed in quotation marks,
which indicate a string constant. Often, the right time to do this is
at the beginning of execution, before any input is processed,
so that the very first record is read with the proper separator.
To do this, use the special BEGIN pattern
(see The BEGIN and END Special Patterns).
For example:
awk 'BEGIN { RS = "u" }
{ print $0 }' mail-list
changes the value of RS to ‘u’, before reading any input.
The new value is a string whose first character is the letter “u”; as a result, records
are separated by the letter “u”. Then the input file is read, and the second
rule in the awk program (the action with no pattern) prints each
record. Because each print statement adds a newline at the end of
its output, this awk program copies the input
with each ‘u’ changed to a newline. Here are the results of running
the program on mail-list:
$ awk 'BEGIN { RS = "u" }
> { print $0 }' mail-list
-| Amelia 555-5553 amelia.zodiac -| sq -| e@gmail.com F -| Anthony 555-3412 anthony.assert -| ro@hotmail.com A -| Becky 555-7685 becky.algebrar -| m@gmail.com A -| Bill 555-1675 bill.drowning@hotmail.com A -| Broderick 555-0542 broderick.aliq -| otiens@yahoo.com R -| Camilla 555-2912 camilla.inf -| sar -| m@skynet.be R -| Fabi -| s 555-1234 fabi -| s. -| ndevicesim -| s@ -| cb.ed -| F -| J -| lie 555-6699 j -| lie.perscr -| tabor@skeeve.com F -| Martin 555-6480 martin.codicib -| s@hotmail.com A -| Sam -| el 555-3430 sam -| el.lanceolis@sh -| .ed -| A -| Jean-Pa -| l 555-2127 jeanpa -| l.campanor -| m@ny -| .ed -| R -|
Note that the entry for the name ‘Bill’ is not split. In the original data file (see Data files for the Examples), the line looks like this:
Bill 555-1675 bill.drowning@hotmail.com A
It contains no ‘u’, so there is no reason to split the record,
unlike the others, which each have one or more occurrences of the ‘u’.
In fact, this record is treated as part of the previous record;
the newline separating them in the output
is the original newline in the data file, not the one added by
awk when it printed the record!
Another way to change the record separator is on the command line, using the variable-assignment feature (see Other Command-Line Arguments):
awk '{ print $0 }' RS="u" mail-list
This sets RS to ‘u’ before processing mail-list.
Using an alphabetic character such as ‘u’ for the record separator is highly likely to produce strange results. Using an unusual character such as ‘/’ is more likely to produce correct behavior in the majority of cases, but there are no guarantees. The moral is: Know Your Data.
gawk allows RS to be a full regular expression
(discussed shortly; see Record Splitting with gawk). Even so, using
a regular expression metacharacter, such as ‘.’ as the single
character in the value of RS has no special effect: it is
treated literally. This is required for backwards compatibility with
both Unix awk and with POSIX.
Reaching the end of an input file terminates the current input record,
even if the last character in the file is not the character in RS.
(d.c.)
The empty string "" (a string without any characters)
has a special meaning
as the value of RS. It means that records are separated
by one or more blank lines and nothing else.
See Multiple-Line Records for more details.
If you change the value of RS in the middle of an awk run,
the new value is used to delimit subsequent records, but the record
currently being processed, as well as records already processed, are not
affected.
After the end of the record has been determined, gawk
sets the variable RT to the text in the input that matched
RS.
Previous: Record Splitting with Standard awk, Up: How Input Is Split into Records [Contents][Index]
4.1.2 Record Splitting with gawk ¶
When using gawk, the value of RS is not limited to a
one-character string. If it contains more than one character, it is
treated as a regular expression
(see Regular Expressions). (c.e.)
In general, each record
ends at the next string that matches the regular expression; the next
record starts at the end of the matching string. This general rule is
actually at work in the usual case, where RS contains just a
newline: a record ends at the beginning of the next matching string (the
next newline in the input), and the following record starts just after
the end of this string (at the first character of the following line).
The newline, because it matches RS, is not part of either record.
When RS is a single character, RT
contains the same single character. However, when RS is a
regular expression, RT contains
the actual input text that matched the regular expression.
If the input file ends without any text matching RS,
gawk sets RT to the null string.
The following example illustrates both of these features.
It sets RS equal to a regular expression that
matches either a newline or a series of one or more uppercase letters
with optional leading and/or trailing whitespace:
$ echo record 1 AAAA record 2 BBBB record 3 |
> gawk 'BEGIN { RS = "\n|( *[[:upper:]]+ *)" }
> { print "Record =", $0,"and RT = [" RT "]" }'
-| Record = record 1 and RT = [ AAAA ] -| Record = record 2 and RT = [ BBBB ] -| Record = record 3 and RT = [ -| ]
The square brackets delineate the contents of RT, letting you
see the leading and trailing whitespace. The final value of
RT is a newline.
See A Simple Stream Editor for a more useful example
of RS as a regexp and RT.
If you set RS to a regular expression that allows optional
trailing text, such as ‘RS = "abc(XYZ)?"’, it is possible, due
to implementation constraints, that gawk may match the leading
part of the regular expression, but not the trailing part, particularly
if the input text that could match the trailing part is fairly long.
gawk attempts to avoid this problem, but currently, there’s
no guarantee that this will never happen.
Caveats When Using Regular Expressions for RS |
|---|
|
Remember that in Record splitting with regular expressions works differently than
regexp matching with the |
The use of RS as a regular expression and the RT
variable are gawk extensions; they are not available in
compatibility mode
(see Command-Line Options).
In compatibility mode, only the first character of the value of
RS determines the end of the record.
mawk has allowed RS to be a regexp for decades.
As of October, 2019, BWK awk also supports it. Neither
version supplies RT, however.
RS = "\0" Is Not Portable |
|---|
|
There are times when you might want to treat an entire data file as a
single record. The only way to make this happen is to give You might think that for text files, the NUL character, which
consists of a character with all bits equal to zero, is a good
value to use for BEGIN { RS = "\0" } # whole file becomes one record?
Almost all other It happens that recent versions of See Reading a Whole File at Once for an interesting way to read
whole files. If you are using |
Next: Nonconstant Field Numbers, Previous: How Input Is Split into Records, Up: Reading Input Files [Contents][Index]
4.2 Examining Fields ¶
When awk reads an input record, the record is
automatically parsed or separated by the awk utility into chunks
called fields. By default, fields are separated by whitespace,
like words in a line.
Whitespace in awk means any string of one or more spaces,
TABs, or newlines; other characters
that are considered whitespace by other languages
(such as formfeed, vertical tab, etc.) are not considered
whitespace by awk.
The purpose of fields is to make it more convenient for you to refer to
these pieces of the record. You don’t have to use them—you can
operate on the whole record if you want—but fields are what make
simple awk programs so powerful.
You use a dollar sign (‘$’)
to refer to a field in an awk program,
followed by the number of the field you want. Thus, $1
refers to the first field, $2 to the second, and so on.
(Unlike in the Unix shells, the field numbers are not limited to single digits.
$127 is the 127th field in the record.)
For example, suppose the following is a line of input:
This seems like a pretty nice example.
Here the first field, or $1, is ‘This’, the second field, or
$2, is ‘seems’, and so on. Note that the last field,
$7, is ‘example.’. Because there is no space between the
‘e’ and the ‘.’, the period is considered part of the seventh
field.
NF is a predefined variable whose value is the number of fields
in the current record. awk automatically updates the value
of NF each time it reads a record. No matter how many fields
there are, the last field in a record can be represented by $NF.
So, $NF is the same as $7, which is ‘example.’.
If you try to reference a field beyond the last
one (such as $8 when the record has only seven fields), you get
the empty string. If used in a numeric operation, you get zero.23
The use of $0, which looks like a reference to the “zeroth” field, is
a special case: it represents the whole input record. Use it
when you are not interested in specific fields.
Here are some more examples:
$ awk '$1 ~ /li/ { print $0 }' mail-list
-| Amelia 555-5553 amelia.zodiacusque@gmail.com F
-| Julie 555-6699 julie.perscrutabor@skeeve.com F
This example prints each record in the file mail-list whose first field contains the string ‘li’.
By contrast, the following example looks for ‘li’ in the entire record and prints the first and last fields for each matching input record:
$ awk '/li/ { print $1, $NF }' mail-list
-| Amelia F
-| Broderick R
-| Julie F
-| Samuel A
Next: Changing the Contents of a Field, Previous: Examining Fields, Up: Reading Input Files [Contents][Index]
4.3 Nonconstant Field Numbers ¶
A field number need not be a constant. Any expression in
the awk language can be used after a ‘$’ to refer to a
field. The value of the expression specifies the field number. If the
value is a string, rather than a number, it is converted to a number.
Consider this example:
awk '{ print $NR }'
Recall that NR is the number of records read so far: one in the
first record, two in the second, and so on. So this example prints the first
field of the first record, the second field of the second record, and so
on. For the twentieth record, field number 20 is printed; most likely,
the record has fewer than 20 fields, so this prints a blank line.
Here is another example of using expressions as field numbers:
awk '{ print $(2*2) }' mail-list
awk evaluates the expression ‘(2*2)’ and uses
its value as the number of the field to print. The ‘*’
represents multiplication, so the expression ‘2*2’ evaluates to four.
The parentheses are used so that the multiplication is done before the
‘$’ operation; they are necessary whenever there is a binary
operator24
in the field-number expression. This example, then, prints the
type of relationship (the fourth field) for every line of the file
mail-list. (All of the awk operators are listed, in
order of decreasing precedence, in
Operator Precedence (How Operators Nest).)
If the field number you compute is zero, you get the entire record.
Thus, ‘$(2-2)’ has the same value as $0.
Similarly, string expressions that evaluate to zero also yield the entire
record. For example, ‘$"answer"’, ‘$"0foo"’, or even
‘$("foo" "bar")’.
Negative field
numbers are not allowed; trying to reference one usually terminates
the program. (The POSIX standard does not define
what happens when you reference a negative field number. gawk
notices this and terminates your program. Other awk
implementations may behave differently.)
As mentioned in Examining Fields,
awk stores the current record’s number of fields in the built-in
variable NF (also see Predefined Variables). Thus, the expression
$NF is not a special feature—it is the direct consequence of
evaluating NF and using its value as a field number.
Next: Specifying How Fields Are Separated, Previous: Nonconstant Field Numbers, Up: Reading Input Files [Contents][Index]
4.4 Changing the Contents of a Field ¶
The contents of a field, as seen by awk, can be changed within an
awk program; this changes what awk perceives as the
current input record. (The actual input is untouched; awk never
modifies the input file.)
Consider the following example and its output:
$ awk '{ nboxes = $3 ; $3 = $3 - 10
> print nboxes, $3 }' inventory-shipped
-| 25 15
-| 32 22
-| 24 14
...
The program first saves the original value of field three in the variable
nboxes.
The ‘-’ sign represents subtraction, so this program reassigns
field three, $3, as the original value of field three minus ten:
‘$3 - 10’. (See Arithmetic Operators.)
Then it prints the original and new values for field three.
(Someone in the warehouse made a consistent mistake while inventorying
the red boxes.)
For this to work, the text in $3 must make sense
as a number; the string of characters must be converted to a number
for the computer to do arithmetic on it. The number resulting
from the subtraction is converted back to a string of characters that
then becomes field three.
See Conversion of Strings and Numbers.
When the value of a field is changed (as perceived by awk), the
text of the input record is recalculated to contain the new field where
the old one was. In other words, $0 changes to reflect the altered
field. Thus, this program
prints a copy of the input file, with 10 subtracted from the second
field of each line:
$ awk '{ $2 = $2 - 10; print $0 }' inventory-shipped
-| Jan 3 25 15 115
-| Feb 5 32 24 226
-| Mar 5 24 34 228
...
It is also possible to assign contents to fields that are out of range. For example:
$ awk '{ $6 = ($5 + $4 + $3 + $2)
> print $6 }' inventory-shipped
-| 168
-| 297
-| 301
...
We’ve just created $6, whose value is the sum of fields
$2, $3, $4, and $5. The ‘+’ sign
represents addition. For the file inventory-shipped, $6
represents the total number of parcels shipped for a particular month.
Creating a new field changes awk’s internal copy of the current
input record, which is the value of $0. Thus, if you do ‘print $0’
after adding a field, the record printed includes the new field, with
the appropriate number of field separators between it and the previously
existing fields.
This recomputation affects and is affected by
NF (the number of fields; see Examining Fields).
For example, the value of NF is set to the number of the highest
field you create.
The exact format of $0 is also affected by a feature that has not been discussed yet:
the output field separator, OFS,
used to separate the fields (see Output Separators).
Note, however, that merely referencing an out-of-range field
does not change the value of either $0 or NF.
Referencing an out-of-range field only produces an empty string. For
example:
if ($(NF+1) != "")
print "can't happen"
else
print "everything is normal"
should print ‘everything is normal’, because NF+1 is certain
to be out of range. (See The if-else Statement
for more information about awk’s if-else statements.
See Variable Typing and Comparison Expressions
for more information about the ‘!=’ operator.)
It is important to note that making an assignment to an existing field
changes the
value of $0 but does not change the value of NF,
even when you assign the empty string to a field. For example:
$ echo a b c d | awk '{ OFS = ":"; $2 = ""
> print $0; print NF }'
-| a::c:d
-| 4
The field is still there; it just has an empty value, delimited by the two colons between ‘a’ and ‘c’. This example shows what happens if you create a new field:
$ echo a b c d | awk '{ OFS = ":"; $2 = ""; $6 = "new"
> print $0; print NF }'
-| a::c:d::new
-| 6
The intervening field, $5, is created with an empty value
(indicated by the second pair of adjacent colons),
and NF is updated with the value six.
Decrementing NF throws away the values of the fields
after the new value of NF and recomputes $0.
(d.c.)
Here is an example:
$ echo a b c d e f | awk '{ print "NF =", NF;
> NF = 3; print $0 }'
-| NF = 6
-| a b c
CAUTION: Some versions of
awkdon’t rebuild$0whenNFis decremented. Until August, 2018, this included BWKawk; fortunately his version now handles this correctly.
Finally, there are times when it is convenient to force
awk to rebuild the entire record, using the current
values of the fields and OFS. To do this, use the
seemingly innocuous assignment:
$1 = $1 # force record to be reconstituted print $0 # or whatever else with $0
This forces awk to rebuild the record. It does help
to add a comment, as we’ve shown here.
There is a flip side to the relationship between $0 and
the fields. Any assignment to $0 causes the record to be
reparsed into fields using the current value of FS.
This also applies to any built-in function that updates $0,
such as sub() and gsub()
(see String-Manipulation Functions).
Understanding $0 |
|---|
|
It is important to remember that It is a common error to try to change the field separators
in a record simply by setting But this does not work, because nothing was done to change the record itself. Instead, you must force the record to be rebuilt, typically with a statement such as ‘$1 = $1’, as described earlier. |
Next: Reading Fixed-Width Data, Previous: Changing the Contents of a Field, Up: Reading Input Files [Contents][Index]
4.5 Specifying How Fields Are Separated ¶
The field separator, which is either a single character or a regular
expression, controls the way awk splits an input record into fields.
awk scans the input record for character sequences that
match the separator; the fields themselves are the text between the matches.
In the examples that follow, we use the bullet symbol (•) to represent spaces in the output. If the field separator is ‘oo’, then the following line:
moo goo gai pan
is split into three fields: ‘m’, ‘•g’, and ‘•gai•pan’. Note the leading spaces in the values of the second and third fields.
The field separator is represented by the predefined variable FS.
Shell programmers take note: awk does not use the
name IFS that is used by the POSIX-compliant shells (such as
the Unix Bourne shell, sh, or Bash).
The value of FS can be changed in the awk program with the
assignment operator, ‘=’ (see Assignment Expressions).
Often, the right time to do this is at the beginning of execution
before any input has been processed, so that the very first record
is read with the proper separator. To do this, use the special
BEGIN pattern
(see The BEGIN and END Special Patterns).
For example, here we set the value of FS to the string
":":
awk 'BEGIN { FS = ":" } ; { print $2 }'
Given the input line:
John Q. Smith: 29 Oak St.: Walamazoo: MI 42139
this awk program extracts and prints the string
‘•29•Oak•St.’.
Sometimes the input data contains separator characters that don’t separate fields the way you thought they would. For instance, the person’s name in the example we just used might have a title or suffix attached, such as:
John Q. Smith: LXIX: 29 Oak St.: Walamazoo: MI 42139
The same program would extract ‘•LXIX’ instead of
‘•29•Oak•St.’.
If you were expecting the program to print the
address, you would be surprised. The moral is to choose your data layout and
separator characters carefully to prevent such problems.
(If the data is not in a form that is easy to process, perhaps you
can massage it first with a separate awk program.)
- Whitespace Normally Separates Fields
- Using Regular Expressions to Separate Fields
- Making Each Character a Separate Field
- Working With Comma Separated Value Files
- Setting
FSfrom the Command Line - Making the Full Line Be a Single Field
- Field-Splitting Summary
4.5.1 Whitespace Normally Separates Fields ¶
Fields are normally separated by whitespace sequences
(spaces, TABs, and newlines), not by single spaces. Two spaces in a row do not
delimit an empty field. The default value of the field separator FS
is a string containing a single space, " ". If awk
interpreted this value in the usual way, each space character would separate
fields, so two spaces in a row would make an empty field between them.
The reason this does not happen is that a single space as the value of
FS is a special case—it is taken to specify the default manner
o