|
Softpanorama |
||||||
| Contents | Bulletin | Scripting in shell and Perl | Network troubleshooting | History | Humor | |
| News | Perl power tools | Recommended Links | Syntax | split command | Examples | Reference | Netcat |
| Perl re-implemenations | tail | head | tac command | AWK | Tips | Humor | Etc |
The cat command concatenates and display files. Netcat can be used instead of cat in case networking capabilities are important. There is a version of cat called dog which adds to cat some networking capabilities like ability to access URLs not only plain files. Source package of dog can be retrieved from Cygwin and compiled on any platform. It is also available on Debian. There are also split and csplit commands, The former split file(s) into equal chunks, the latter splits file(s) based on context. Head and tail command can be used to select certain area of the file. There is also a version of cat called zcat which performs operation similar to uncompress -c or gunzip -c on each input file
Even classic cat is a more versatile tool then it looks from the first site. For example it can be used as a primitive editor for creation one liners or adding a line to the config file.
Cat accepts multiple file as an arguments and reads each file in the order listed and writes the content of merged stream to the standard output. For example
cat file prints file on your terminal (Note: cat -n file will do the same with line numbering)
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 capability is often used to create small files with cat instead of editor like vi or ed. For example
cat > /etc/resolv.conf
> line > line > line Ctrl-DYou can also add a line to the file if you use ">>" instead of ">":
cat >> /etc/group
If you're appending only a single line, it's better to use echo instead of
cat
Few Unix users know that cat can number lines so in a way it can serve as a preprocessor to more to view files with line numbering. For example:
cat -n var/adm/messages | more
|
With option -n cat can number lines |
cat can show nonprinting characters, such as tabs and control
characters. For instance,
to see if any line has trailing blanks:
$ cat -A input.txt This line has trailing blanks. $ This line does not.$ $
General syntax is
cat [ options ] [file...]
Solaris version is less feature rich then GNU version. Also in Solaris version option -s has different semantic then in GNU version.
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.
Command:
cat -n myfile
writes the contents of the file myfile to standard output with each line numbered.
Command:
cat doc1 doc2 > doc.all
concatenates the files doc1 and doc2 and writes the result to doc.all.
The command:
cat start - middle - end > filewhen 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 > filebecause 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.
cat f - g
cat section1.1 section1.2 section1.3 >section1
This command creates a file named section1 that is a copy of section1.1 followed by section1.2 and section1.3.
To suppress error messages about files that do not exist, enter:
cat -q section2.1 section2.2 section2.3 >section2
If section2.1 does not exist, this command concatenates section2.2 and section2.3. The result is the same if you do not use the -q flag, except that the cat command displays the error message:
cat: cannot open section2.1You may want to suppress this message with the -q flag when you use the cat command in shell procedures.
cat section1.4 >> section1
The >> (two carets) appends a copy of section1.4 to the end of section1. If you want to replace the file, use the > (caret).
cat >> notes Get milk on the way home Ctrl-D
This command adds Get milk on the way home to the end of the file called notes. The cat command does not prompt; it waits for you to enter text. Press the Ctrl-D key sequence to indicate you are finished.
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:
|
|
||||
| Bulletin | Latest | Past week | Past month |
|
#!/usr/local/bin/perl -wT
#
# $Id: cat,v 1.1.1.1 2001/06/06 08:54:05 sdague Exp $
#
# $Log: cat,v $
# Revision 1.1.1.1 2001/06/06 08:54:05 sdague
# initial import
#
# Revision 1.1.1.1 2001/05/13 19:55:38 sdague
# added initial import of PPT work
#
# Revision 1.1 1999/02/26 03:21:14 abigail
# Initial revision
#
#
use strict;
use Getopt::Std;
my ($VERSION) = '$Revision: 1.1.1.1 $' =~ /([.\d]+)/;
# Get the options.
# Print a usuage message on a unknown option.
# Requires my patch to Getopt::Std of 25 Feb 1999.
$SIG {__WARN__} = sub {
if (substr ($_ [0], 0, 14) eq "Unknown option") {
$0 =~ s{.*/}{};
print <) {
if ($squeeze_empty) {
my $is_empty = /^$/;
if ($is_empty && $was_empty) {
next;
}
$was_empty = $is_empty;
}
$_ = sprintf "%6d $_", ++ $count if $number_lines ||
$number_non_blanks && /\S/;
$_ =~ s/$/\$/ if $ends;
if ($nonprinting) {
$_ =~ s/([\x80-\xFF])/"M-" . ("\x7F" & $1)/ge;
$_ =~ s/([\x00-\x08\x0B-\x1F])/"^" . chr (0100 + ord $1)/ge;
$_ =~ s/\x7F/^?/g;
}
if ($tabs) {
$_ =~ s/\x09/^I/g;
}
print;
}
__END__
=pod
=head1 NAME
cat -- concatenate and print files.
=head1 SYNOPSIS
cat [-benstuv] [file ...]
=head1 DESCRIPTION
Ireads and prints the files in order they are given. If no files
are given, Iis processed. A lone dash represents
Ias well.
=head2 OPTIONS
Iaccepts the following options:
=over 4
=item -b
Number all the non blank lines, starting at 1.
=item -e
Print a dollar sign (B<$>) at the end of each lines. Implies I<-v>.
=item -n
Number all the lines, starting at 1.
=item -s
The Ioption. Sequential empty lines are squeezed into a
single empty line.
=item -t
Display tabs as I<^I>. Implies I<-v>.
=item -u
Unbuffer output.
=item -v
Display non-printable characters in a printable way. Characters in the
range I<\000> - I<\037>, with the exception of tabs and linefeeds, are
printed as I<^X>, where Iis the symbol I<\0100> higher. I is
printed as I<^?>. Characters whose highest bit is set are printed as
I, followed by the representation of the character with the high
bit stripped.
=back
=head1 ENVIRONMENT
The working of Iis not influenced by any environment variables.
=head1 BUGS
Ihas no known bugs.
=head1 STANDARDS
This Iimplementation is compliant with the B
specification, also known as B.
This Iimplementation is compatible with the B implementation.
=head1 REVISION HISTORY
$Log: cat,v $
Revision 1.1.1.1 2001/06/06 08:54:05 sdague
initial import
Revision 1.1.1.1 2001/05/13 19:55:38 sdague
added initial import of PPT work
Revision 1.1 1999/02/26 03:21:14 abigail
Initial revision
=head1 AUTHOR
The Perl implementation of Iwas written by Abigail, I .
=head1 COPYRIGHT and LICENSE
This program is copyright by Abigail 1999.
This program is free and open software. You may use, copy, modify, distribute
and sell this program (and any modified variants) in any way you wish,
provided you do not restrict others to do the same.
=cut
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 ...
What if you wanted to reverse the order of lines in a file? That is the job of the
taccommand. (Note thattaciscatspelled backwards.) It reverses the order of the lines or fields in a list of files.It does not reverse the order of files -- this you must do yourself by listing them in reverse order after the
taccommand. For an example of howtacworks, compare results ofls -l | tailandls -l | tail | tacon some files in your home directory.Questions or comments? I'd love to hear from you -- send mail to jacek@artymiak.com.
Next time, we'll take a look at the
sortandtsortcommands. See you then!
Example 1: View Compressed File and Uncompress with zcatCompressing a file using gzip creates a compressed file with *.gz extension. You can view a compressed file with zcat with the following way. Which would be as same as the uncompressed file operation ‘cat filename’. zcat uncompresses the file and shows it in the stdout.
$ zcat filename.gz | more$ ls -l big-file.* -rw-r--r-- 1 ramesh ramesh 24853275 May 9 15:14 big-file.txt $ gzip big-file.txt [Note: Compress the file] $ ls -l big-file.* -rw-r--r-- 1 ramesh ramesh 9275204 May 9 15:14 big-file.txt.gz $ zcat big-file.txt.gz [Note: View the file without uncompressing it] zcat big-file.txt.gz > big-file.txt [Note: Uncompress the file]Example 2: View a gzipped file which don’t have the gz suffix.You can uncompress a gzipped file which don’t have the gz suffix. If you try to uncompress a gzipped file which don’t have the gz suffix with “gunzip” or “gzip -d” command you will face the following error.
gunzip: auth.log: unknown suffix -- ignoredBut this zcat will uncompress the file and shows the content as shown below.
$ cat > test-file.txt This is a test file used for gunzip and zcat testing zcat is awesome command. $ gzip test-file.txt $ mv test-file.txt.gz test-file-no-ext $ gzip -d test-file-no-ext gzip: test-file-no-ext: unknown suffix -- ignored $ zcat test-file-no-ext This is a test file used for gunzip and zcat testing zcat is awesome command.Example 3: Display the file content without worrying about whether it is compressed or not
When you are not sure whether a file is compressed or not, you can still view the file without worrying about it’s compression status as shown below.
In this example, If the input-file is compressed zcat will display the content by uncompressing it. If the input-file is not compressed zcat will display the content as it is.$ zcat -f input-fileExample 4: Paging the compressed file with zless / zmore.
You can paginate a compressed file with zless command or zmore command as shown below.
$ zcat filename.gz | more $ zcat filename.gz | less (or) $ zless filename.gz $ zmore filename.gzNote: To open any kind of file type, refer to our previous article Open & View 10 Different File Types with Linux Less Command – The Ultimate Power of Less.
Expands a compressed file to standard output.
zcat [ -n ] [ -V ] [ File ... ]
The zcat command allows the user to expand and view a compressed file without uncompressing that file. The zcat command does not rename the expanded file or remove the .Z extension. The zcat command writes the expanded output to standard output.
Flags
Parameters
File ... Specifies the compressed files to expand. Return Values
If the zcat command exits with a status of 1 if any of the following events occur:
- The input file was not produced by the compress command.
- An input file cannot be read or an output file cannot be written.
If no error occurs, the exit status is 0.
Exit Status
0 Successful completion. >0 An error occurred. Examples
To view the foo.Z file without uncompressing it, enter:
zcat foo.ZThe uncompressed contents of the foo.Z file are written to standard output. The file is not renamed.
Related Information
The compress command, pack command, uncompress command, unpack command.
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:-)
Use
Listing 1. Using cat to concatenate files and standard input streamscat, 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.$ (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 >> fileIf 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/hostsTo 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.
dog -- enhanced cat with networking capabilities
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.
Most popular humor pages:
Manifest of the Softpanorama IT Slacker Society : Ten Commandments of the IT Slackers Society : Computer Humor Collection : BSD Logo Story : The Cuckoo's Egg : C++ Humor : ARE YOU A BBS ADDICT? : Object oriented programmers of all nations : C Humor : Financial Humor : Financial Humor Bulletin, 2008 : Financial Humor Bulletin, 2010 : Richard Stallman Related Humor : Admin Humor : Perl-related Humor : Linus Torvalds Related humor : PseudoScience Related Humor : Networking Humor : Shell Humor: Financial Humor Bulletin, 2011 : Financial Humor Bulletin, 2012 : Financial Humor Bulletin, 2013 : Java Humor : Software Engineering Humor : Sun Solaris Related Humor : The Most Comprehensive Collection of Editor-related Humor : Microsoft plans to buy Catholic Church : Education Humor : IBM Humor : Assembler-related Humor : VIM Humor Computer Viruses Humor : Bright tomorrow is rescheduled to a day after tomorrow : Classic Computer Humor : Best Russian Programmer Humor : Russian Musical Humor : The Perl Purity Test : Politically Incorrect Humor : GPL-related Humor : OFM Humor : IDS Humor : Real Programmers Humor : Scripting Humor : Web Humor : Programming Language Humor : Goldman Sachs related humor : Greenspan humor :
Copyright © 1996-2013 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. This document is an industrial compilation designed and created exclusively for educational use and is distributed under the Softpanorama Content License. Site uses AdSense so you need to be aware of Google privacy policy. Original materials copyright belong to respective owners. Quotes are made for educational purposes only in compliance with the fair use doctrine. This is a Spartan WHYFF (We Help You For Free) site written by people for whom English is not a native language. Grammar and spelling errors should be expected. The site contain some broken links as it develops like a living tree...
|
|
You can use PayPal to make a contribution, supporting hosting of this site with different providers to distribute and speed up access. Currently there are two functional mirrors: softpanorama.info (the fastest) and softpanorama.net. |
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: January 27, 2013