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. /^\.Ah/{ p s/"//g 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" Comment .Ah "Substitution" Substitution .Ah "Delete" Delete .Ah "Append, Insert and Change" Append, Insert and Change .Ah "List" 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.