Полезная информация

UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 30.21 Per File Setups in Separate Files Chapter 30
vi Tips and Tricks
Next: 30.23 Safer vi Filter-Throughs

30.22 Filtering Text Through a UNIX Command

Warning! When you're editing in vi, you can send a block of text as standard input to a UNIX command. The output from this command replaces the block of text in the buffer. You can filter text through a command from either ex or vi. The main difference between the two methods is that you indicate the block of text with line addresses in ex and with text objects (movement commands) in vi.

30.22.1 Filtering Text with ex

The first example demonstrates how to filter text with ex. Assume that you're editing a file with a list of names and you want to alphabetize (sort) lines 96 through 99. You simply type the addresses of the lines you want to filter, followed by an exclamation mark and the UNIX command to be executed. For example, the command:


will pass lines 96 through 99 through the sort (36.1) filter and replace those lines with the output of sort.

The ex % operator is the easiest way to filter all the lines in your buffer. If you're editing a C program, for instance, you could feed it all through the formatting program named indent by typing:


30.22.2 Filtering Text with vi

In vi, text is filtered through a UNIX command by typing an exclamation mark (!) followed by any of vi's movement keystrokes that indicate a block of text, and then by the UNIX command line to be executed. For example:


will pass the next sentence through command.

There are a couple of unusual features about how vi acts when you use this structure.

As another example, assume you have a portion of text in a message that you're going to send to the Usenet (1.33) "net news." On Usenet, text that might be offensive or would give away the answer to a question is "rotated." The letter a is changed to n, b is changed to o, and so on. There are programs for rotating text, but it's also easy to rotate text with the tr (35.11) command. In this example, the second sentence is the block of text that will be filtered to the command.

 One sentence before.
 With a screen editor you can scroll the page
 move the cursor, delete lines, insert characters, 
 and more, while seeing the results of your edits 
 as you make them.
 One sentence after.
 One sentence after.

An exclamation mark appears on the last line to prompt you for the UNIX command.

tr '[a-z]' '[A-Z]'
 One sentence before.  
 One sentence after.

Enter the UNIX command and press RETURN. The input is replaced by the output.

To repeat the previous command, the syntax is:

! object !

It is sometimes useful to send sections of a coded document to nroff to be replaced by formatted output. Remember that the "original" input is replaced by the output. Fortunately, if there is a mistake, such as an error message being sent instead of the expected output, you can undo the command and restore the lines.

- TOR from O'Reilly & Associates' Learning the vi Editor, Chapter 7

Previous: 30.21 Per File Setups in Separate Files UNIX Power ToolsNext: 30.23 Safer vi Filter-Throughs
30.21 Per File Setups in Separate Files Book Index30.23 Safer vi Filter-Throughs

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