<<

. 12
( 69 .)



>>

„ Type head and a space. – The shell displays the first – The shell displays as many
of lines you want to display,
ten lines of the file. and a space. lines of the file as you specify.



44
4
WORK WITH TEXT


You can also set the tail command to display any new lines added to the end of a
file, by using the -f option.
The Mac OS X operating system maintains a number of files known as log files.
These files record system activity, much of it invisible to the user, which you can
use to diagnose problems or monitor processes. New lines are regularly appended
to log files. You can find most of the important system logs in the /var/log directory.
You can monitor these log files by using the tail -f command. To stop
monitoring these files, press Control + C.

Example:
[ferro:˜] user% tail -5 -f /var/log/system.log
Dec 2 16:40:03 ferro configd[105]: executing
/System/Library/SystemConfiguration/Kicker.bundle/Resources/restart-AppleTalk
Dec 2 16:40:03 ferro lookupd[533]: lookupd (version 272) starting - Mon Dec
2 16:40:03 2002
Dec 2 16:40:05 ferro root: setting hostname to ferro.local.
Dec 2 16:40:05 ferro configd[105]: executing
/System/Library/SystemConfiguration/Kicker.bundle/Resources/set-hostname
Dec 2 16:40:06 ferro root: setting hostname to ferro.local.
^C
[ferro:˜] user%




¤ Type the filename and ‹ Type the filename and
VIEW THE END OF A TEXT FILE START IN THE MIDDLE
OF A TEXT FILE
press Return. press Return.
„ Type tail and a space.
„ Type tail and a space.
– The shell displays the last – The shell displays the file
¤ Type a plus symbol, the
ten lines of the file. starting at the line you
specify.
number of the line you want
to start from, and a space.

45
UNIX FOR MAC



EXTRACT TEXT FROM A FILE
around your regular expression pattern with the grep

Y
ou can find and display lines within a text file that
command.
match a specific pattern with the grep command.
The grep command searches through a file to find
Options for the grep command include -i, which makes
lines that match a regular expression. Regular expressions,
grep consider upper- and lowercase letters the same for
or regexps, are commonly used to match text patterns.
matching purposes; -v, which inverts the search and lists
Regular expressions are more powerful and flexible than
lines that do not match; and -l, which does not display
normal glob patterns because they can match a variety of
matches but instead lists the names of files whose contents
patterns and you can combine them to form very complex
contain the regular expression pattern.
expressions.
A simple but useful example of the power of the grep
The syntax for the grep command is:
command is that it can help you with crossword puzzles or
crossword-style games such as Scrabble. Using the list of
grep 'pattern' filename
English words in /usr/share/dict/words, you can display all
lines matching a specific pattern. For example, if the first
You do not need to enclose the regular expression pattern
letter in a four-letter word is k, the third is n, and you do
in single quotes. However, if you do not use the single
not know the rest, you can use this command to display all
quotes, the shell interprets the pattern as a glob pattern
possible matches as show below:
and matches it against files in the current directory. For this
reason, it is always a good practice to use single quotes
grep -i '^k.n.$' /usr/share/dict/words


EXTRACT TEXT FROM A FILE




‹ Type a filename and press ‹ Type a dollar sign, a
EXTRACT TEXT USING CHEAT ON CROSSWORD
THE GREP COMMAND PUZZLES
Return. closing quote mark, a space,
„ Type grep and a space. „ Type grep -i, a space, a /usr/share/dict/words, and
– The shell displays all lines press Return.
single quote, and a caret.
¤ Type a regular expression in the file that match the
– The shell displays all
¤ Type each letter you
pattern.
pattern enclosed in single
words that might fit.
quotes, and a space. know, and type a period for
each letter you do not know.
46
4
WORK WITH TEXT




A period (.) matches any character. A set of characters
Regular expressions consist of ordinary text
in square brackets, such as [0123456789ABCDEF],
and characters with special meanings. The
matches any of those characters, and a range in
simplest regular expression is just plain text,
brackets matches any character within that range,
such as Kynn. This matches any line that
such as [a-zA-Z].
contains the word Kynn anywhere on the line.
To match more than one character of the same type,
To select matches at the beginning of a line,
you can use the plus symbol (+) after a special
you can use the special character caret (^).
character, normal character, or range. To match zero
The regular expression to match lines starting
or more of the preceding types of characters, use an
with Kynn is ^Kynn. Likewise, the dollar sign
asterisk (*). The regular expression '.*' matches
($) indicates the end of a line, as with Kynn$,
anything, because it is zero or more of any character.
which only matches lines where Kynn is the
A question mark (?) indicates zero or one repetition
final text of that line.
of the preceding character.




‹ Type a filename and press ‹ Type an asterisk wildcard
INVERT A GREP SEARCH LIST ALL FILES CONTAINING
A PATTERN
Return. to search all files, and press
„ Type grep -v and a space.
„ Type grep -l and a space. Return.
– The grep command
¤ Type a regular expression
– The shell displays the
¤ Type a regular expression
displays all lines that do
pattern in quotes for lines you
not match your pattern. names of all files that contain
pattern enclosed in single
do not want to match, and a
the pattern.
quotes, and a space.
space.

47
UNIX FOR MAC



REDIRECT TEXT TO A FILE
information at any given time. For example, if you want to

Y
ou can save the text output of any shell command by
record your disk usage on a certain date, you can type
redirecting the output to a file. Unix shell commands
command du -s > du-2003-03-05.
normally display their output in the Terminal shell
window, immediately after you type the command. This
When you redirect text, the shell does not print any special
display method is known as standard output. Using output
messages on-screen; after you complete the command, you
redirection, you can change the destination of the standard
only see the prompt again. You can use other commands,
output; it does not have to appear in the Terminal window.
such as cat, grep, or head, to examine the text file that
you create.
To save the output of a command into a file, you can use a
greater-than symbol (>):
You can combine output redirection with the cat
command to create a new file from two or more files. You
command and arguments > output-file
do this by using the following command:
For example, to save the output from the command grep
cat filename1 filename2 filename3 >
'Apple' CHANGES.txt into a file named apple-
composite-file
changes.txt, you can use this command:
The new composite-file consists of the text contained in
grep 'Apple' CHANGES.txt > apple-changes.txt
each of the named files, in the order listed on the command
line. You can concatenate as many files as you like.
You can use the grep command to record the output of a
command for later processing, such as editing or printing
the file. It is also an effective way of making a record of

REDIRECT TEXT TO A FILE




– The shell saves the output
¤ Type a greater-than
SAVE COMMAND OUTPUT
symbol and a space. in the file you create.
„ Type a Unix shell
‹ Type the name of the file
command that displays text
output, and a space. you want to create and press
Return.

48
4
WORK WITH TEXT


If you redirect output to an existing file, you overwrite that
file with the new text output. This can be dangerous, because
indiscriminately overwriting files can lead to loss of data.
You can instruct the shell to prevent output redirection
from overwriting existing files by typing the command set
noclobber. The shell prints an error message saying that the
destination file exists, and the command does not execute.
If you have the noclobber command set and you need to
overwrite an existing file, you can type an exclamation point
(!), or bang, after the greater-than symbol. This causes the
redirection to occur even if the file exists.

Example:
[ferro:˜] user% head /var/log/system.log > log
[ferro:˜] user% set noclobber
[ferro:˜] user% tail /var/log/system.log > log
log: File exists.
[ferro:˜] user% tail /var/log/system.log >! log
[ferro:˜] user%




– The cat command
‹ Type a greater-than
CONCATENATE TWO TEXT FILES
symbol, a space, and the concatenates the file and
„ Type cat and a space. name of the file you want to saves the output in the file
¤ Type the name of two or create. you create.
more files, separated by a
› Press Return.
space.

49
UNIX FOR MAC



CHAIN TEXT COMMANDS TOGETHER
command. Some commands do not automatically read

<<

. 12
( 69 .)



>>