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

sed & awk

sed & awkSearch this book
Previous: 5.7 Transform Chapter 5
Basic sed Commands
Next: 5.9 Print Line Number

5.8 Print

The print command (p ) causes the contents of the pattern space to be output. It does not clear the pattern space nor does it change the flow of control in the script. However, it is frequently used before commands (d , N , b ) that do change flow control. Unless the default output is suppressed (-n ), the print command will cause duplicate copies of a line to be output. It can be used when default output is suppressed or when the flow control through the program avoids reaching the bottom of the script.

Let's look at a script that shows how the print command might be used for debugging purposes. It is used to show what the line looked like before you made any changes.

#n Print line before and after changes.
s/^\.Ah //p

Note that the print flag is supplied to the substitute command. The substitute command's print flag differs from the print command in that it is conditional upon a successful substitution.

Here's a sample run of the above script:

$ sed -f sed.debug ch05

.Ah "Comment"
.Ah "Substitution"
.Ah "Delete"
.Ah "Append, Insert and Change"
Append, Insert and Change
.Ah "List"

Each affected line is printed twice.

We'll see additional examples of the print command in the next chapter. See also the multiline print command (P ) in the next chapter.

Previous: 5.7 Transform sed & awk Next: 5.9 Print Line Number
5.7 Transform Book Index 5.9 Print Line Number

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