Softpanorama
May the source be with you, but remember the KISS principle ;-)

Contents Bulletin Scripting in shell and Perl Network troubleshooting History Humor

cat command

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-D
You 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...]

GNU Cut Options:

Solaris Cut Options:

Solaris version is less feature rich then GNU version. Also in Solaris version option -s has different semantic then in GNU version.

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.

Examples

Example 1 Concatenating a file

Example 2: Concatenating two files into one

Example 3: Concatenating several files with text entered on keyboard

The command:

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.

Example 4:  Output f's contents, then standard input, then g's contents.

 cat f - g

Example 5: To concatenate several files:

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.1

You may want to suppress this message with the -q flag when you use the cat command in shell procedures.

Example 6: To append one file to the end of another:

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).

Example 7: To add text to the end of a file:

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.

Environmental variables and exit status

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:


Top updates

Bulletin Latest Past week Past month
Google Search


NEWS CONTENTS

Old News ;-)

cat reimplmentation from Perl Power Tools

#!/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

I reads and prints the files in order they are given. If no files
are given, I is processed. A lone dash represents
I as well.

=head2 OPTIONS

I accepts 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 I option. 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 I is 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 I is not influenced by any environment variables.

=head1 BUGS

I has no known bugs.

=head1 STANDARDS

This I implementation is compliant with the B
specification, also known as B.

This I implementation 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 I was 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

Tip Concatenating files with cat


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
...

Tip Reading text streams in chunks with head and tail

Reversing files with tac

What if you wanted to reverse the order of lines in a file? That is the job of the tac command. (Note that tac is cat spelled 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 tac command. For an example of how tac works, compare results of ls -l | tail and ls -l | tail | tac on 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 sort and tsort commands. See you then!

The Power of Z Commands – Zcat, Zless, Zgrep, Zdiff Examples

Example 1: View Compressed File and Uncompress with zcat

Compressing 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 -- ignored

But 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-file

Example 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.gz

Note: 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.

zcat Command

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

-n Omits the compressed file header from the compressed file.

Note:

Use the -n option if the file was compressed using the -n option.
-V Writes the current version and compile options to standard error.

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:

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.Z

The 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.

Useless Use of Cat Award

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 cat on a single file to view it from the command line is a valid use of cat (but you might be better off if you get accustomed to using less for 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:

	{ foo; bar; cat mumble; baz } | whatever
Here, the contents of the file mumble are output to stdout after the output from the programs foo and bar, and before the output of baz. All the generated output is piped to the program whatever. (Read up on shell programming constructs if this was news to you :-)

[ Jan 16, 2007 ] Save time with text editing one-liners by Michael Stutz (stutz@dsl.org)

Edits with cat

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
$ 

Add text to the end of a file

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 echo instead of cat:

$ echo "192.255.255.255     bigblue" >> /etc/hosts

To append lines of text that are itemized beginning with 1, use cat's -n option; lines are preceded with the line number (offset with up to five space characters) and a tab character. Add the -b option 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 cat by 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.

Show nonprinting characters

cat has 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.

Recommended Links

dog -- enhanced cat with networking capabilities

Reference

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.



Etc

Society

Groupthink : Understanding Micromanagers and Control Freaks : Toxic Managers : BureaucraciesHarvard Mafia : Diplomatic Communication : Surviving a Bad Performance Review : Insufficient Retirement Funds as Immanent Problem of Neoliberal Regime : PseudoScience : Who Rules America : Two Party System as Polyarchy : Neoliberalism  : The Iron Law of Oligarchy : Libertarian Philosophy

Quotes

Skeptical Finance : John Kenneth Galbraith : Keynes : George Carlin : Skeptics : Propaganda  : SE quotes : Language Design and Programming Quotes : Random IT-related quotes : Oscar Wilde : Talleyrand : Somerset Maugham : War and Peace : Marcus Aurelius : Eric Hoffer : Kurt Vonnegut : Otto Von Bismarck : Winston Churchill : Napoleon Bonaparte : Ambrose Bierce : Oscar Wilde : Bernard Shaw : Mark Twain Quotes

Bulletin:

Vol 26, No.1 (January, 2013) Object-Oriented Cult : Vol 25, No.12 (December, 2013) Rational Fools vs. Efficient Crooks: The efficient markets hypothesis : Vol 25, No.08 (August, 2013) Cloud providers as intelligence collection hubs : Vol 23, No.10 (October, 2011) An observation about corporate security departments : Vol 23, No.11 (November, 2011) Softpanorama classification of sysadmin horror stories : Vol 25, No.05 (May, 2013) Corporate bullshit as a communication method : Vol 25, No.10 (October, 2013) Cryptolocker Trojan (Win32/Crilock.A) : Vol 25, No.06 (June, 2013) A Note on the Relationship of Brooks Law and Conway Law

History:

Fifty glorious years (1950-2000): the triumph of the US computer engineering : Donald Knuth : TAoCP and its Influence of Computer Science : Richard Stallman : Linus Torvalds  : Larry Wall  : John K. Ousterhout : CTSS : Multix OS Unix History : Unix shell history : VI editor : History of pipes concept : Solaris : MS DOSProgramming Languages History : PL/1 : Simula 67 : C : History of GCC developmentScripting Languages : Perl history   : OS History : Mail : DNS : SSH : CPU Instruction Sets : SPARC systems 1987-2006 : Norton Commander : Norton Utilities : Norton Ghost : Frontpage history : Malware Defense History : GNU Screen : OSS early history

Classic books:

The Peter Principle : Parkinson Law : 1984 : The Mythical Man-MonthHow to Solve It by George Polya : The Art of Computer Programming : The Elements of Programming Style : The Unix Hater’s Handbook : The Jargon file : The True Believer : Programming Pearls : The Good Soldier Svejk : The Power Elite

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 : IT Slang : C++ Humor : ARE YOU A BBS ADDICT? : The Perl Purity Test : Object oriented programmers of all nations : Financial Humor : Financial Humor Bulletin, 2008 : Financial Humor Bulletin, 2010 : The Most Comprehensive Collection of Editor-related Humor : Programming Language Humor : Goldman Sachs related humor : Greenspan humor : C Humor : Scripting Humor : Real Programmers Humor : Web Humor : GPL-related Humor : OFM Humor : Politically Incorrect Humor : IDS Humor : "Linux Sucks" Humor : Russian Musical Humor : Best Russian Programmer Humor : Microsoft plans to buy Catholic Church : 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 : Education Humor : IBM Humor : Assembler-related Humor : VIM Humor : Computer Viruses Humor : Bright tomorrow is rescheduled to a day after tomorrow : Classic Computer Humor

 

The Last but not Least


Copyright © 1996-2014 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: February 19, 2014