A reader writes:
I had taken an intro course to UNIX a few years ago, but have found your book Teach Yourself Unix in
24 Hours MUCH more comprehensive and user-aware!I have hit a stumbling block in Chapter 16 (page 337) where you illustrate the use of cat, however. You show:
$ cat average.awk NF > 2 { count += 1 totalsize += $5 }There is more that follows on this page and page 338. This seems to illustrate that the cat command allows you to input text into the named file. Perhaps that is not what you meant. Did I miss something?
The reader continues with a nice post-script:
I think you book is quite a resource for introducing our people to UNIX. The Information Systems department here in our office has purchased 16 copies and distributed them to my colleagues.
First off, thanks for the kind words about my book. It distills more years of working with the Unix command line than I want to think about (and yet, even this morning I spent two hours working with grep and vi through an ssh connection to my server).
In terms of the cat command, it’s really one of the simpler commands available on the command line in Unix/Linux and just dumps out the contents of the specified file to standard output. So by default, cat .login, for example, dumps the contents of the “.login” file to the screen.
Once you start looking at file redirection in the shell, however, then you find out that cat becomes a considerably more powerful utility. For example, if I typed in cat .login > my.login then I would have accomplished essentially the same functionality as a file copy command (e.g., cp). But since cat can accept multiple filenames, a classic use of the command is to merge a bunch of files into a single output filename: cat a.txt b.txt c.txt d.txt > merged.txt.
Another quite common use of cat occurs in shell scripts, where it uses what us shell scripting programmers call here documents. These are a way to trick commands into thinking that you’re redirecting input from a file, as in wc < inputfile when there’s actually no file being fed to the command. The notation looks like this: << some end marker so if I wanted to find out what wc had to report about a paragraph of text, I could use:
cat << EOF | wc This is a paragraph of text that we want to learn more about, so we're feeding it to the 'wc' command through use of a here document. EOF
The result of this would be that wc would report how many lines, words and characters were found in its standard input, e.g., the short paragraph of text fed to it through use of the here document.
I hope that clears everything up! If not, please post a response and we can explore this topic further.
hello sir, i understood how cat works, but dint get the last part cat <<EOF | wc thing.. And can i get the shell script of cat?
See i am working on GRIB files (i.e) Grided Binary files.I have gone through a web site where it was me ntioned that if you use this command ” cat > EOF ” then you will be able to cat all the files there.
So can you please let me know as to how this command works..