Next
The next command (n) outputs the contents of the pattern space and then reads the next line of input without returning to the top of the script. Its syntax is:
[address]nThe next command changes the normal flow control, which doesn’t output the contents of the pattern space until the bottom of the script is reached and which always begins at the top of the script after reading in a new line. In effect, the next command causes the next line of input to replace the current line in the pattern space. Subsequent commands in the script are applied to the replacement line, not the current line. If the default output has not been suppressed, the current line is printed before the replacement takes place.
Let’s look at an example of the next command in which we delete a blank line only when it follows a pattern matched on the previous line. In this case, a writer has inserted a blank line after a section heading macro (.H1). We want to remove that blank line without removing all blank lines in the file. Here’s the sample file:
.H1 "On Egypt" Napoleon, pointing to the Pyramids, said to his troops: "Soldiers, forty centuries have their eyes upon you."
The following script removes that blank line:
/^\.H1/{
n
/^$/d
}You can read this script as follows: “Match any line beginning with the string `.H1', then print that line and read in the next line. If that line is blank, delete it.” The braces are used to apply multiple commands at the same address.
In a longer script, you must remember that ...
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.
Read now
Unlock full access