Filters: Doing It Your Way

A look at several of the more flexible filters, probrams that read some input, perform some operation on it, and write the altered data as output.
Working with Filenames

Often, many files have the same basic name, but different extensions. For example, suppose we have a TeX file foo.tex. Then we could very well have associated files foo.aux, foo.bib, foo.dvi, foo.ps, foo.idx, foo.log, etc. You might want a script to be able to process these files, given the name of the file foo.tex. The basename utility:

basename foo.tex .tex

will give you the basic name foo. If we have a shell variable containing the name of the TeX file, we might use

basename ${TEXFILE} .tex

Again, there is more than way of getting the basename of a file: you could do this in sed using:

echo ${TEXFILE} | sed 's/.tex$//'

Whichever approach we take, we can construct the name of the other files once we know the basic name. For example, we can get the name of the log file by:

LOGFILE=`basename ${TEXFILE} .tex`.log

This is very useful: I use vi for most of my editing, and it allows you to get at the name of the file currently being edited in a macro; % is replaced with the filename. If I'm editing a TeX file foo.tex, and I want to preview the dvi file using xdvi, I can transform % (let's call it foo.tex) into foo.dvi automatically in a macro

:!xdvi `basename % .tex`.dvi &

I can bind this to a function key and never worry about the name of the dvi file when I want to view it, by adding this line to my .exrc file:

map ^R :!xdvi `basename % .tex`.dvi &^M^M

The ^R and ^M characters are added by typing Control-V Control-R and Control-V Control-M, respectively, assuming you are editing your .exrc file with vi.

Conclusion

In this article, we have looked at the some of the tools available in Linux for filtering text. We have seen how using these filters we can manipulate the output of one command so it is in a more convenient form to be used as the input for another program or to be read by a human. This kind of task arises naturally in a lot of shell-based work, both in scripts and on the command line, so it is a handy skill to have. Although the man pages for sed and awk can be a little cryptic, solutions to problems can often be very simple. With a little practice, you can do quite a lot.

______________________

Free Dummies Books
Continuous Engineering

Learn:

  • What continuous engineering is
  • How to continuously improve complex product designs
  • How to anticipate and respond to markets and clients
  • How to get the most out of your engineering resources

Get your free book now

Sponsored by IBM

Free Dummies Books
Service Virtualization

Learn to:

  • Define service virtualization
  • Select the most beneficial services to virtualize
  • Improve your traditional approach to testing
  • Deliver higher-quality software faster

Get your free book now

Sponsored by IBM