|
Softpanorama |
May the source be with you, but remember the KISS principle ;-)
|
| News | Syntax | Recommended Links | Sorting algorithms | Recommended Papers | Rcut | Reference | Pipes |
| Perl re-implemenations | uniq | sort | tr | AWK | Tips | Humor | Etc |
The cat utility concatenates and display files. It also can be used as a primitive editor for creation one liners or adding a line to the config file.
Also it can number lines so in a way it can serve as a preprocessor to more. for example:
cat -n var/adm/messages | more
Solaris version is less feature rich then GNU version. Also Solaris version and GNU version assign option -s different semantic. It reads each file in sequence and writes it on the standard output. Thus: cat file prints file on your terminal, and:
cat file1 file2 >file3
concatenates file1 and file2, and writes the results in file3. If no input file is given, cat reads from the standard input file. That permits to create small files with cat instead of editor like vi or ed
General syntax is
cat [-nbsuvet] [file...]
The following options are supported:
DEL character (octal 0177)
is printed ^?. Other non-printable characters are printed as M-x,
where x is the ASCII character specified by the low-order seven
bits.When used with the -v option, the following options may be used:
The -e and -t options are ignored if the -v option is not specified.
The following operand is supported:
See largefile(5) for the description of the behavior of cat when encountering files greater than or equal to 2 Gbyte ( 231 bytes).
The following command:
cat -n myfile
writes the contents of the file myfile to standard output with each line numbered.
The following command:
cat doc1 doc2 > doc.all
concatenates the files doc1 and doc2 and writes the result to doc.all.
The command:
example% cat start - middle - end > file
when standard input is a terminal, gets two arbitrary pieces of input from the terminal with a single invocation of cat. Note, however, that if standard input is a regular file, this would be equivalent to the command:
cat start - middle /dev/null end > file
because the entire contents of the file would be consumed by cat the first time `-' was used as a file operand and an end-of-file condition would be detected immediately when `-' was referenced the second time.
See environ(5) for descriptions of the following environment variables that affect the execution of cat: LC_CTYPE, LC_MESSAGES, and NLSPATH.
The following exit values are returned:
See attributes(5) for descriptions of the following attributes:
The venerable Randal L. Schwartz hands out Useless Use of Cat Awards from time to time; you can see some recent examples in Deja News. (The subject line really says "This Week's Useless Use of Cat Award" although the postings are a lot less frequent than that nowadays). The actual award text is basically the same each time, and the ensuing discussion is usually just as uninteresting, but there are some refreshing threads there among all the flogging of this dead horse.
The oldest article Deja News finds is from 1995, but it's actually a followup to an earlier article. By Internet standards, this is thus an Ancient Tradition.
Exercise: Try to find statistically significant differences between the followups from 1995 and the ones being posted today.
(See below for a reconstruction of the Award text.)
Briefly, here's the collected wisdom on using cat:
The purpose of cat is to concatenate (or "catenate") files. If it's only one file, concatenating it with nothing at all is a waste of time, and costs you a process.The fact that the same thread ("but but but, I think it's cleaner / nicer / not that much of a waste / my privelege to waste processes!") springs up virtually every time the Award is posted is also Ancient Usenet Tradition.Of course, as Heiner points out, using
caton a single file to view it from the command line is a valid use ofcat(but you might be better off if you get accustomed to usinglessfor this instead).In a recent thread on
comp.unix.shell, the following example was posted by Andreas Schwab as another Useful Use of Cat on a lone file:Here, the contents of the file{ foo; bar; cat mumble; baz } | whatevermumbleare output to stdout after the output from the programsfooandbar, and before the output ofbaz. All the generated output is piped to the programwhatever. (Read up on shell programming constructs if this was news to you:-)
Take a quick look at some essential editing one-liners that can save you time and effort. Text-editing operations are normally done interactively, inside a text editor application. Some tasks, however, can be accomplished quickly and easily, right from the UNIX® command line. What's more, these one-liners can be used in scripts to automate various editing procedures.Most UNIX® developers settle on Emacs, vi, or one of the many variants, offshoots, and clones of these two text-editing applications. Files are normally opened in the editor of choice, and changes are interactively specified and applied to the file by the operator.
But you can often do an editing job at the command line more quickly than it takes to open the file in a text editor. A complex editing procedure can be programmed and specified from the command line and executed across multiple files, eliminating all unnecessary screen display, cursor motion, and manual interaction with the files. A good tactic is to keep a cache of relevant one-liners on hand to do common editing jobs. Not only do they save you time, especially in batch operations involving multiple files, but you can also use them in scripts.
One-liners for editing and processing text are a famous tradition in the Perl and AWK (and lately Ruby) languages and, of course, the shell. This article demonstrates basic text-editing techniques with three of the most primary command-line editing tools readily available on all systems:
cat,ed, andsed. The editing examples that follow start with the simplest and most common constructs and work up to the more complex.Use
cat, whose name stands for together, to concatenate files and standard input streams, as in Listing 1. The slackers of the world also use it as a general pager (cat file) and a complete text-editing environment (cat > file). Its syntax is unrivaled in its simplicity and, for text editing one-liners, it gives you quick ways to append or insert text without an editor.
Listing 1. Using cat to concatenate files and standard input streams
$ (cat - input1 - input2 - input3 - input4) | mailx ted Ted, Take a look at these example files. This is the first file ... Ctrl-D This is the second file ... Ctrl-D This is the third file -- note the fourth paragraph below ... Ctrl-D And here's the last file ... Ctrl-D $
The slackers are on to something, though. When you need to append text to the end of a file, there's nothing quicker than
cat:
$ cat >> file > line > line > line Ctrl-D $
While you're adding lines, pressing Ctrl-U erases the current line, Ctrl-Z suspends the process, and Ctrl-C aborts everything. When you're done, press Ctrl-D on a line of its own. (These are some of the default Korn shell control keys, but they work for most shells and editing modes.)
If the data you're entering is an X selection that you're pasting from another window, this one-liner is generally quicker to use than calling up an editor, opening the target file, moving to the end of the file, pasting the selection, saving the file, and exiting the editor. It can also be more useful when you're pasting formatted or specially formatted text, and you want to keep the formatting because some text editors and editing modes reformat the X selection when you paste it.
Although this operation is a common, everyday practice, you always have to be careful that you use the shell operator for appending redirection (
>>) and not the regular redirection operator (>); if you mistakenly use the latter, you'll overwrite the contents of the file with the text you mean to append.To add the entire contents of one file to the end of another file, give the filename:
$ cat footnotes.txt >> file
If you're appending only a single line instead of multiple lines or an entire file, you can use
echoinstead ofcat:
$ echo "192.255.255.255 bigblue" >> /etc/hosts
To append lines of text that are itemized beginning with 1, use
cat's-noption; lines are preceded with the line number (offset with up to five space characters) and a tab character. Add the-boption to suppress the numbering of blank lines:
$ cat -nb > file This line is numbered And so is this Another numbered line Ctrl-D $ cat file 1 This line is numbered 2 And so is this 3 Another numbered line $
Insert text at the beginning of a file
You can insert text at the beginning of a file with
catby specifying the standard input with a hyphen (-) and writing to a new file:
$ cat - file > newfile This is the beginning of the file And then the old file is inserted Below this line: Ctrl-D $
Although it's simple, the disadvantage of this one-liner is that it creates a new file. If you want to insert text into the original file, the renaming shuffle you have to do makes this almost more trouble than it's worth. Better ways are just ahead with
ed.
cathas several useful options. Some of them control the way it outputs nonprinting characters, such as tabs and control characters. To determine whether a file or a group of text files has embedded control characters, use these options. For instance, if a file has trailing blanks, you can see them:
$ cat -vet input.txt This line has trailing blanks. $ This line does not.$ $
These options differ according to your UNIX implementation; Table 1 gives the standard IBM AIX® operating system options.
Gnu version options
-A, --show-all
equivalent to -vET
-b, --number-nonblank
number nonblank output lines
-e equivalent to -vE
-E, --show-ends
display $ at end of each line
-n, --number
number all output lines
-s, --squeeze-blank
never more than one single blank line
-t equivalent to -vT
-T, --show-tabs
display TAB characters as ^I
-u (ignored)
-v, --show-nonprinting
use ^ and M- notation, except for LFD and TAB
--help display this help and exit
--version
output version information and exit
With no FILE, or when FILE is -, read standard input.
Copyright © 1996-2007 by Dr. Nikolai Bezroukov. www.softpanorama.org was created as a service to the UN Sustainable Development Networking Programme (SDNP) in the author free time. Submit comments This document is an industrial compilation designed and created exclusively for educational use and is placed under the copyright of the Open Content License(OPL). Original materials copyright belong to respective owners. Quotes are made for educational purposes only in compliance with the fair use doctrine.
Standard disclaimer: The statements, views and opinions presented on this web page are those of the author and are not endorsed by, nor do they necessarily reflect, the opinions of the author present and former employers, SDNP or any other organization the author may be associated with. We do not warrant the correctness of the information provided or its fitness for any purpose.
Last modified: March 19, 2008