Manipulating Files with cat


Red Hat Linux has a utility that can help you keep short lists, gather lists together, and even show you information about your system. The utility is called cat, short for concatenate, which means to combine files.

The cat command will also display the contents of an entire file on the screen (for example, type cat filename.txt). If the file is fairly long, it will quickly scroll past you on the screen. To prevent this, use the cat filename.txt | less command.

Using the pipe (|) and less command together displays the file one page at a time. You can then use the up and down arrow keys to move backward and forward through the pages. For more on using pipes to combine two separate functions, see the “Pipes and Pagers” section later in this chapter.

Using Redirection

Redirection means causing the shell to change what it considers to be standard input or where the standard output should be going.

To redirect standard output, use the > symbol. Placing > after the cat command (or after any utility or application that writes to standard output) will direct its output to the filename following the symbol.

For example, using cat by itself simply outputs whatever you input to the screen as if it were repeating the line you just typed. The following example shows cat repeating every line that is entered:

[sam@halloween sam]$cat buy some sneakers buy some sneakers then go to the coffee shop then go to the coffee shop then buy some coffee then buy some coffee

To redirect cat output to a file, type the following at a shell prompt (pressing the Enter key takes you to the next blank line):

[sam@halloween sam]$cat > sneakers.txt buy some sneakers then go to the coffee shop then buy some coffee

Press Enter to go to an empty line and use the Ctrl-D keys to quit cat.

Note that in Figure 8-5 there are no repeated entries. That is because the standard output from cat was redirected. That redirection was to a brand new file you made called sneakers.txt. You can find the file in the directory you were in when you started cat (type ls if you want to see it listed).

click to expand
Figure 8-5. Joining Files and Redirecting Output

As you learned earlier, you can then use cat to read the file. At the prompt, type:

 cat sneakers.txt 
Caution

Be careful when you redirect the output to a file, because you can easily overwrite an existing file! Make sure the name of the file you are creating does not match the name of a pre-existing file, unless you want to replace it.

Use output redirection again for another file and call it home.txt. For this example, type the following:

[sam@halloween sam]$cat > home.txt bring the coffee home take off shoes put on sneakers make some coffee relax! 

Now, on an empty line, use the Ctrl-D key combination again to quit cat. Next, use cat to join home.txt with sneakers.txt and redirect the output of both files to a brand new file called saturday.txt (you will find an example in Figure 8-5). Type the following:

[sam@halloween sam]$cat sneakers.txt home.txt > saturday.txt

You can see that cat has added home.txt where sneakers.txt ended.

Appending Standard Output

You can use output redirection to add new information to the end of an existing file. Similar to when you used the > symbol, you tell your shell to send the information somewhere other than standard output.

However, when you use >>, you are adding information to a file, rather than replacing the contents of a file entirely.

The best explanation is a demonstration. Take two files which have already been created in previous examples (sneakers.txt and home.txt) and join them by using the append output symbol (>>). You want to add the information in home.txt to the information already in sneakers.txt, so type the following:

cat home.txt >> sneakers.txt

Now check the file using the command cat sneakers.txt. The final output shows the contents of home.txt at the end of the file.

[sam@halloween sam]$cat sneakers.txt buy some sneakers then go to the coffee shop then buy some coffee bring the coffee home take off shoes put on sneakers make some coffee relax! [sam@halloween sam]$

The command you typed told the system to append the output from the file home.txt to the file sneakers.txt.

By appending the output, you save yourself time (and a bit of disk clutter) by using existing files instead of creating a new file.

Compare the results of the files sneakers.txt and saturday.txt now, and you will see that they are identical. To make your comparison, type the following:

cat sneakers.txt; cat saturday.txt

The contents of both files will be displayed — first sneakers.txt, then saturday.txt.

click to expand
Figure 8-6. Stringing Commands and Comparing Files

Redirecting Standard Input

Not only can you redirect standard output, you can perform the same type of redirection with standard input.

When you use the redirect standard input symbol < , you are telling the shell that you want a file to be read as input for a command.

Use a file you have already created to demonstrate this idea. Just type the following:

cat < sneakers.txt

Because you used the less-than symbol (<) to separate the cat command from the file, the output of sneakers.txt was read by cat.




The Red Hat Documentation Team - Official Red Hat Linux User's Guide
The Red Hat Documentation Team - Official Red Hat Linux User's Guide
ISBN: N/A
EAN: N/A
Year: 2002
Pages: 223

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net