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

UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 1.5 Anyone Can Program the Shell Chapter 1
Next: 1.7 Power Grows on You

1.6 Power Tools for Editing

My wife won't let me buy a power saw. She is afraid of an accident if I use one. So I rely on a hand saw for a variety of weekend projects like building shelves. However, if I made my living as a carpenter, I would have to use a power saw. The speed and efficiency provided by power tools would be essential to being productive.

For people who create and modify text files, sed (34.24) and awk (33.11) are power tools for editing. Most of the things that you can do with these programs can be done interactively with a text editor. However, using sed and awk can save many hours of repetitive work in achieving the same result.

sed and awk are peculiar and it takes time to learn them, but the capabilities they provide can repay the learning many times over, especially if text editing is a normal part of your trade.

Both of these programs let you write editing scripts to do many of the things that you might otherwise do laboriously with repeated commands in an editor like vi (30.2).

Even more important, they let you do edits on data that is streaming through UNIX pipes - (43.21, 27.15, 18.9, 17.17) data that may never be written back into a file.

However, the primary motivation for learning sed and awk is that they are useful for devising general solutions to text editing problems. For some people, myself included, the satisfaction of solving a problem is the difference between work and drudgery. Given the choice of using vi or sed to make a series of repeated edits over a number of files, I will choose sed, simply because it makes the problem more interesting to me. I am refining a solution instead of repeating a series of keystrokes. Besides, once I accomplish my task, I congratulate myself on being clever. I feel like I have done a little bit of magic and spared myself some dull labor.

Initially, using sed and awk will seem like the long way to accomplish a task. After several attempts you may conclude that the task would have been easier to do manually. Be patient. You not only have to learn how to use sed and awk but you also need to learn to recognize situations where using them pays off. As you become more proficient, you will not only solve problems more quickly, you will solve a broader range of problems.

- DD from O'Reilly & Associates' sed & awk, Chapter 1

Previous: 1.5 Anyone Can Program the Shell UNIX Power ToolsNext: 1.7 Power Grows on You
1.5 Anyone Can Program the Shell Book Index1.7 Power Grows on You

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