home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  

sed & awk

sed & awkSearch this book
Previous: 5.8 Print Chapter 5
Basic sed Commands
Next: 5.10 Next

5.9 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/{

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

        if(  rand_type  ==  TYPE_0  )  {
        if(  rand_type  ==  TYPE_0  )  state[ -1 ] = rand_type;
        if(  n  <  BREAK_1  )  {
                if(  n  <  BREAK_3  )  {
        if(  rand_type  ==  TYPE_0  )  state[ -1 ] = rand_type;
        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.

Previous: 5.8 Print sed & awk Next: 5.10 Next
5.8 Print Book Index 5.10 Next

The UNIX CD Bookshelf Navigation The UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System