Skip to Content
sed & awk, 2nd Edition
book

sed & awk, 2nd Edition

by Dale Dougherty, Arnold Robbins
March 1997
Intermediate to advanced
432 pages
11h 31m
English
O'Reilly Media, Inc.
Content preview from sed & awk, 2nd Edition

Print Line Number

An equal sign (=) following an address prints the line number of the matched line. Unless you suppress the automatic output of lines, both the line number and the line itself will be printed. Its syntax is:

[line-address]=

This command cannot operate on a range of lines.

A programmer might use this to print certain lines in a source file. For instance, the following script prints the line number followed by the line itself for each line containing a tab followed by the string “if”. Here’s the script:

#n print line number and line with if statement
/       if/{
=
p
}

Note that #n suppresses the default output of lines. Now let’s see how it works on a sample program, random.c:

$ sed -f sedscr.= random.c
192
        if(  rand_type  ==  TYPE_0  )  {
234
        if(  rand_type  ==  TYPE_0  )  state[ -1 ] = rand_type;
236
        if(  n  <  BREAK_1  )  {
252
                if(  n  <  BREAK_3  )  {
274
        if(  rand_type  ==  TYPE_0  )  state[ -1 ] = rand_type;
303
        if(  rand_type  ==  TYPE_0  )  state[ -1 ] = rand_type;

The line numbers might be useful in finding problems reported by the compiler, which typically lists the line number.

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

The AWK Programming Language, 2nd Edition

The AWK Programming Language, 2nd Edition

Alfred V. Aho, Brian W. Kernighan, Peter J. Weinberger

Publisher Resources

ISBN: 1565922255Supplemental ContentErrata Page