Table Of Contentsed & awk
sed & awk
Second Edition
Dale Dougherty and Arnold Robbins
Beijing• Cambridge• Farnham• Köln• Sebastopol • Tokyo
sed & awk, Second Edition
by Dale Dougherty and Arnold Robbins
Copyright © 1997, 1990 O’Reilly Media, Inc. All rights reserved.
Printed in the United States of America.
Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North,
Sebastopol, CA 95472.
O’ReillyMedia,Inc.booksmaybepurchasedforeducational,business,orsalespromotionaluse.
Online editions are also available for most titles (safari.oreilly.com). For more informationcontact
our corporate/institutional sales department: 800-998-9938 orcorporate@oreilly.com.
Editor: Tim O’Reilly
Update Editor: Gigi Estabrook
Production Editor: Nicole Gipson Arigo
Printing History:
November 1990: First Edition.
March 1991: Minor corrections.
July 1992: Minor corrections.
November 1992: Minor corrections.
March 1997: Second edition.
NutshellHandbook,theNutshellHandbooklogo,andtheO’Reillylogoareregisteredtrademarks
of O’Reilly Media, Inc.sed & awk, Second Edition, the image of a slender loris, and related trade
dressaretrademarksofO’ReillyMedia,Inc.Manyofthedesignationsusedbymanufacturersand
sellerstodistinguishtheirproductsareclaimedastrademarks.Wherethosedesignationsappearin
this book, and O’Reilly Media, Inc. was aware of a trademark claim, the designations have been
printed in caps or initial caps.
While every precaution has been taken in the preparation of this book, the publisher and the
author(s) assume no responsibility for errors or omissions, or for damages resulting from the use
of the information contained herein.
This book uses RepKover™, a durable and flexible lay-flat binding.
ISBN: 978-1-565-92225-9
[M] [2/10]
To Miriam, for your love and patience.
—Arnold Robbins
Ta ble of Contents
Preface ..................................................................................................................... xi
1: Pow er Tools for Editing ............................................................................. 1
May You Solve Interesting Problems ................................................................. 1
A Str eam Editor ................................................................................................... 3
A Patter n-Matching Pr ogramming Language ..................................................... 5
Four Hurdles to Mastering sed and awk ........................................................... 6
2: Understanding Basic Operations .......................................................... 8
Awk, by Sed and Grep, out of Ed ..................................................................... 8
Command-Line Syntax ..................................................................................... 13
Using sed .......................................................................................................... 15
Using awk ......................................................................................................... 20
Using sed and awk Together ........................................................................... 23
3: Understanding Regular Expression Syntax .................................... 26
That’s an Expression ........................................................................................ 27
A Line-Up of Characters ................................................................................... 29
I Never Metacharacter I Didn’t Like ................................................................ 54
4: Writing sed Scripts .................................................................................... 55
Applying Commands in a Script ...................................................................... 56
A Global Perspective on Addressing ............................................................... 58
Testing and Saving Output .............................................................................. 61
vii
viii Table of Contents
Four Types of sed Scripts ................................................................................ 64
Getting to the PromiSed Land ......................................................................... 77
5: Basic sed Commands .............................................................................. 78
About the Syntax of sed Commands ............................................................... 78
Comment ........................................................................................................... 79
Substitution ....................................................................................................... 80
Delete ................................................................................................................ 86
Append, Insert, and Change ............................................................................ 87
List ..................................................................................................................... 90
Transfor m .......................................................................................................... 93
Print ................................................................................................................... 94
Print Line Number ............................................................................................ 95
Next ................................................................................................................... 95
Reading and Writing Files ................................................................................ 96
Quit ................................................................................................................. 104
6: Advanced sed Commands ................................................................... 106
Multiline Pattern Space .................................................................................. 107
A Case for Study ............................................................................................. 116
Hold That Line ................................................................................................ 119
Advanced Flow Control Commands ............................................................. 127
To Join a Phrase ............................................................................................. 133
7: Writing Scripts for awk ........................................................................ 137
Playing the Game ........................................................................................... 138
Hello, World ................................................................................................... 138
Awk’s Programming Model ............................................................................ 139
Patter n Matching ............................................................................................ 141
Records and Fields ......................................................................................... 143
Expr essions ..................................................................................................... 147
System Variables ............................................................................................. 151
Relational and Boolean Operators ................................................................ 157
For matted Printing .......................................................................................... 163
Passing Parameters Into a Script ................................................................... 166
Infor mation Retrieval ..................................................................................... 169