 | Level: Introductory Jacek Artymiak (jacek@artymiak.com), Freelance author and consultant
01 Oct 2002 In our continuing series on GNU text utilities, Jacek Artymiak takes a look at cat -- the command that UNIX lovers love to love, and UNIX haters love to hate.
Often you need to process several files as one and
save the results of such processing to a single output file. The cat (short for "concatenate") command takes one or more
files on its input and prints them to its output as a single file. For example,
cat chapter01 chapter02 chapter03 > book saves
three chapterXX files into a single book file.
The input files are printed in the order they are listed after the cat command, so to reverse the order of information,
you must first reverse the order of input files. Also, when the number of
files that need to be processed is too large for you to type their names by hand,
you may use a wildcard, as in cat chapter* >
book remembering that the names of the files will be sorted in
ascending order. This may cause interesting problems when you
suddenly find out that chapter13 is sent to the
output before
chapter2, while chapter13 is sent to the output after
chapter02.
When the output of cat is not redirected to a
file or another command's standard input, cat
behaves as most command-line tools and send its output to the console.
This means that you can use cat to display
files; for example, you can use cat /etc/passwd to display the
contents of the system password file. For convenience, should should view large files with less, as in less /etc/passwd (you can learn more about less by typing man less).
Although cat is primarily used to concatenate
files, you can also use it for simple automatic processing of input. For
example, you can remove multiple blank lines with a single blank line (use
the -s option), which is a good way to clean up
source code before showing it to the world. Unfortunately, cat doesn't have an option for removing blank lines
altogether. But that's not a big problem, because you can remove them
with the handy sed command:
Listing 1. Using sed with cat to remove blank lines
$ cat -s /etc/X11/XF86Config | sed '/^[[:space:]]*$/d'
...
# Multiple FontPath entries are allowed (they are concatenated together)
# By default, Red Hat 6.0 and later now use a font server independent of
# the X server to render fonts.
FontPath "/usr/X11R6/lib/X11/fonts/TrueType"
FontPath "unix/:7100"
EndSection
...
|
Blank line compression is a handy trick for reading config files and sources
of HTML pages, especially those generated by scripts that insert
gratuitous newlines, as well as sources that contain large
conditional structures whose items have been separated with empty lines.
Another important feature of cat is its ability
to number lines. This functionality comes in handy for program
documentation as well as legal and scientific documentation. Line numbers
printed on the left side make it easy to refer to a certain part of
the document. This is very important in programming, scientific research,
business reporting, or even legislative work. There are two options for
numbering lines: -b for numbering non-blank
lines only and -n for numbering all lines:
Listing 2. Numbering lines
$ cat -b /etc/X11/XF86Config
...
14 # Multiple FontPath entries are allowed (they are concatenated together)
15 # By default, Red Hat 6.0 and later now use a font server independent of
16 # the X server to render fonts.
17 FontPath "/usr/X11R6/lib/X11/fonts/TrueType"
18 FontPath "unix/:7100"
19 EndSection
...
$ cat -n /etc/X11/XF86Config
...
20 # Multiple FontPath entries are allowed (they are concatenated together)
21 # By default, Red Hat 6.0 and later now use a font server independent of
22 # the X server to render fonts.
23
24 FontPath "/usr/X11R6/lib/X11/fonts/TrueType"
25 FontPath "unix/:7100"
26
27 EndSection
...
|
cat can also help when you are viewing files
that contain non-printing character like tabs. You can reveal them with
these options:
-
-T display tabs as ^I
-
-v displays non-printing characters, except line-feed and tab using their "control sequence" equivalents. For example, when you process a file generated on a Windows system, it will use Control-M (^M) characters to mark the end of the line. Characters with codes higher than 127 will be preceded with M- as in "meta", which is an equivalent of Alt- characters on other systems.
-
-E adds the dollar sign ($) at the end of each line.
Listing 3. Displaying non-printing characters
$ cat -t /etc/X11/XF86Config
...
# Multiple FontPath entries are allowed (they are concatenated together)
# By default, Red Hat 6.0 and later now use a font server independent of
# the X server to render fonts.
^IFontPath^I"/usr/X11R6/lib/X11/fonts/TrueType"
^IFontPath^I"unix/:7100"
EndSection
...
$ cat -E /etc/X11/XF86Config
...
# Multiple FontPath entries are allowed (they are concatenated together)$
# By default, Red Hat 6.0 and later now use a font server independent of$
# the X server to render fonts.$
$
FontPath "/usr/X11R6/lib/X11/fonts/TrueType"$
FontPath "unix/:7100"$
$
EndSection$
...
$ cat -v /etc/X11/XF86Config
...
^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@M-|M-8^X^@^@^@
P^@^O"M-X^O M-@^M^@^@^@M-^@^O"M-@M-k^@M-8*^@
@M-^H$M-@M-9|A(M-@)M-yM-|M-sM-*M-hW^A^@^@j^@
M-|M-sM-%1M-@M-9^@^B^@^@M-sM-+fM-^A= ^@ ^@
F^@^@ ^@M-9^@^H^@^@M-sM-$M-G^E(l!M-@M-^?
^IM-A5^@^@^D^@PM-^]M-^\X1M-H%^@^@^D^@tyM-G
...
|
Next time, we'll take a look at head and tail. See you then! In the meantime, if you have questions or comments, I'd love to hear from you -- send e-mail to
jacek@artymiak.com.
Resources
About the author  | |  | Jacek Artymiak works as a freelance consultant, developer, and writer. Since 1991 he's been developing software for many commercial and free variants of UNIX and BSD operating systems (AIX, HP-UX, IRIX, Solaris, Linux, FreeBSD, NetBSD, OpenBSD, and others), as well as MS-DOS, Microsoft Windows, Mac OS, and Mac OS X. Jacek specializes in business and financial application development, Web design, network security, computer graphics, animation, and multimedia. He's a prolific writer on technology subjects and the coauthor of "Install, Configure, and Customize Slackware Linux" (Prima Tech, 2000) and "StarOffice for Linux Bible" (IDG Books, 2000). Many of Jacek's software projects can be found at SourceForge. You can learn more about him at his
personal Web site and contact him at jacek@artymiak.com. |
Rate this page
|  |