cat command Allows you to look, modify or combine a file.
cat filename [-n] [-b] [-u] [-s] [-v]
| filename | The name of the file or files that you wish to look at or perform tasks on. | |
| -n | Precede each line output with its line number. | |
| -b | Number the lines, as -n, but omit the line numbers from blank lines. | |
| -u | The output is not buffered. (The default is buffered output.) | |
| -s | cat is silent about non-existent files. | |
| -v | Non-printing characters (with the exception of tabs, new-lines and form-feeds) are printed visibly. ASCII control characters (octal 000 - 037) are printed as ^n, where n is the corresponding ASCII character in the range octal 100 - 137 (@, A, B, C, . . ., X, Y, Z, [, \, ], ^, and _); the 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. | |
| -e | A $ character will be printed at the end of each line (prior to the new-line). | |
| -t | Tabs will be printed as ^I's and formfeeds to be printed as ^L's. |
cat file1.txt file2.txt > file3.txt
Reads file1.txt and file2.txt and combines those files to make file3.txt.
Now type cat without specifing a file to read
Now type cat without specifing a file to read
% cat
Then type a few words on the keyboard and press the [Return] key.
Finally hold the [Ctrl] key down and press [d] (written as ^D for short) to end the input.
What has happened?
If you run the cat command without specifing a file to read, it reads the standard input (the keyboard), and on receiving the 'end of file' (^D), copies it to the standard output (the screen).
In UNIX, we can redirect both the input and the output of commands.
We use the > symbol to redirect the output of a command. For example, to create a file called list1 containing a list of fruit, type
% cat > list1
Then type in the names of some fruit. Press [Return] after each one.
pear
banana
apple
^D {this means press [Ctrl] and [d] to stop}
banana
apple
^D {this means press [Ctrl] and [d] to stop}
What happens is the cat command reads the standard input (the keyboard) and the > redirects the output, which normally goes to the screen, into a file called list1
To read the contents of the file, type
% cat list1
The form >> appends standard output to a file. So to add more items to the file list1, type
% cat >> list1
Then type in the names of more fruit
peach
grape
orange
^D (Control D to stop)
grape
orange
^D (Control D to stop)
To read the contents of the file, type
% cat list1
You should now have two files. One contains six fruit, the other contains four fruit.
We will now use the cat command to join (concatenate) list1 and list2 into a new file called biglist. Type
% cat list1 list2 > biglist
What this is doing is reading the contents of list1 and list2 in turn, then outputing the text to the file biglist
To read the contents of the new file, type
% cat biglist
We use the < symbol to redirect the input of a command.
The command sort alphabetically or numerically sorts a list. Type
% sort
dog
cat
bird
ape
^D (control d to stop)
cat
bird
ape
^D (control d to stop)
ape
bird
cat
dog
bird
cat
dog
% sort < biglist
and the sorted list will be output to the screen.
To output the sorted list to a file, type,
% sort < biglist > slist
Use cat to read the contents of the file slist
No comments:
Post a Comment