#print More Alteration - Filtering One of the most convenient and powerful features of vi is the ability to take parts of your text and run it through a program. This is called 'filtering' and is quite commonly used with programs like nroff, fmt, spell, sort, uniq, and others. It is much more convenient to use this than to write a portion of the file out, leave the editor, run the program on that portion into a temporary file, re-enter the editor, and read in the contents of the temporary file. The filtering command in vi is '!' and it can take any targets that the 'd', 'c', 'y', '>', or '<' commands can. Thus, to run your whole text through nroff, you would simply type 1G To go to the very beginning of the file !G To filter the whole file through a program. Vi will prompt with a '!' at the bottom of screen. You type nroff and hit return and whammo, off it goes. Your file will be replaced by the nroff output. Use the undo command to go back to the original text afterwards if you wish. Type 'ready' to try out this command. #user #create FilterTest EXERCISE Use the ! command to run the part of this file below the dashed line through the program 'sort'. When you've done that, delete all of these instructions, upto and including the dashed line. Then exit, saving your changes. ----------------------------------------------------------------------------- to.vax135 net.bugs.v7 net.rec.scuba net.columbia net.games.trivia net.trivia net.oa net.news.b net.applic net.music net.misc fa.arms-d fa.railroad fa.arpa-bboard cu.bboard net.bugs.2bsd net.bugs.4bsd fa.space net.space net.eunice net.rec.bridge net.cycle cu.games.empire net.cse net.news.newsite net.games.rogue net.rec.skydive cu.info-bitgraph net.math fa.poli-sci net.general cu.general net.travel net.sport.baseball to.cornell net.lang.apl net.dcom fa.telecom fa.info-cpm net.lang net.games.pacman net.aviation net.ham-radio fa.info-micro net.info-micro net.micro net.rec.photo net.auto fa.editor-p fa.digest-p fa.tcp-ip cu.tcp-ip net.games.dip net.games.frp net.lang.lisp net.lisp net.news.group cu.msggroup net.followup cu.followup net.jokes.q net.rumor net.ucds net.unix-wizards net.rec.birds net.records net.sources net.movies net.jokes net.games cu.games net.wines net.taxes net.bugs net.periphs net.cooks fa.works net.works cu.works net.cms fa.info-terms cu.info-terms fa.sf-lovers net.sf-lovers net.chess fa.human-nets cu.localnets net.news cu.info-pcnet net.test net.auto.vw fa.info-vax cu.info-vax net.usenix net.unix net.sport.hockey fa.energy #create SortedList cu.bboard cu.followup cu.games cu.games.empire cu.general cu.info-bitgraph cu.info-pcnet cu.info-terms cu.info-vax cu.localnets cu.msggroup cu.tcp-ip cu.works fa.arms-d fa.arpa-bboard fa.digest-p fa.editor-p fa.energy fa.human-nets fa.info-cpm fa.info-micro fa.info-terms fa.info-vax fa.poli-sci fa.railroad fa.sf-lovers fa.space fa.tcp-ip fa.telecom fa.works net.applic net.auto net.auto.vw net.aviation net.bugs net.bugs.2bsd net.bugs.4bsd net.bugs.v7 net.chess net.cms net.columbia net.cooks net.cse net.cycle net.dcom net.eunice net.followup net.games net.games.dip net.games.frp net.games.pacman net.games.rogue net.games.trivia net.general net.ham-radio net.info-micro net.jokes net.jokes.q net.lang net.lang.apl net.lang.lisp net.lisp net.math net.micro net.misc net.movies net.music net.news net.news.b net.news.group net.news.newsite net.oa net.periphs net.rec.birds net.rec.bridge net.rec.photo net.rec.scuba net.rec.skydive net.records net.rumor net.sf-lovers net.sources net.space net.sport.baseball net.sport.hockey net.taxes net.test net.travel net.trivia net.ucds net.unix net.unix-wizards net.usenix net.wines net.works to.cornell to.vax135 # vi FilterTest #cmp FilterTest SortedList #succeed Quite impressive! And you should be impressed too, at how simple it was to do that. Now you're good enough to go on to the tricky stuff... #fail Not quite. What you need to do is to move the cursor to the first line after the dashed line. Then type '!G' (to tell vi you want to pipe the rest of the file through a program). Vi will prompt at the bottom of the screen and you type 'sort' and hit return. After a moment, the old, unsorted list should be replaced by a brand spanking new sorted one (hopefully of the same elements). You then go back and delete the instructions (all of them, through the dashed line) using the 'dd' command and Voila!, you're done. Just type :wq to save your changes and exit. When the learn program types a percent sign, just type 'ready' to try it again. #next 6.1 10