sed & awk

sed & awkSearch this book
Previous: 5.3 SubstitutionChapter 5
Basic sed Commands
Next: 5.5 Append, Insert, and Change
 

5.4 Delete

We previously showed examples of the delete command (d). It takes an address and deletes the contents of the pattern space if the line matches the address.

The delete command is also a command that can change the flow of control in a script. That is because once it is executed, no further commands are executed on the "empty" pattern space.[4] The delete command causes a new line of input to be read and a new pass on the editing script to begin from the top. (In this behavior, it is the same as the next command, which you'll encounter later in this chapter.)

[4] UNIX documentation reads "no further commands are attempted on the corpse of a deleted line." R.I.P.

The important thing to remember is: if the line matches the address, the entire line is deleted, not just the portion of the line that is matched. (To delete a portion of a line, use the substitute command and specify an empty replacement.) In the previous chapter, we showed a command to delete blank lines:

/^$/d

Another use of the delete command could be to strip out certain troff requests, such as those that add spacing, break the page, and turn fill mode off and on:

/^\.sp/d
/^\.bp/d
/^\.nf/d
/^\.fi/d

These commands delete an entire line. For instance, the first command will delete the line ".sp 1" or ".sp .03v".

The delete command can be used to delete a range of lines. In the previous chapter, there is an example of deleting all tables from a file by deleting the lines between the .TS and .TE macros. There is also a delete command (D) used to delete a portion of a multiline pattern space. This advanced command is presented in the next chapter.


Previous: 5.3 Substitutionsed & awkNext: 5.5 Append, Insert, and Change
5.3 SubstitutionBook Index5.5 Append, Insert, and Change

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