Lesson_09
Lesson_09
Copyright 2009
2006 Stewart Weiss
Input redirection operator <
There are two other redirection operators, '<< ' and '>>'. We
will start with '>>':
The output append operator, >>, which you saw in an earlier
lesson, forces standard output to be appended to a file instead
of replacing the file's contents. This is very useful; it gives
you the means to add new lines to files.
There are many system programs that maintain log files, i.e.,
files that record the program's activities. Many of these
programs are shell scripts. When they need to append
information to their log files, they use the append operator.
I could create a log file to record whenever I started a bash
shell by adding this to my .bashrc file:
echo "bash started on `tty` at `date`" >> ~/.mylog
This would add a line to the file .mylog in my home
directory each time I ran a bash shell. The expression `...`
is explained next.
The sort program can be used for sorting one or more text
files in sequence. It can also merge files.
In its simplest form,
sort filename
will sort filename using the first field of the line (first
chars up to the first white space) in ASCII collating order,
displaying the sorted file on the screen (standard output).
sort will ignore case by default in some versions of UNIX,
whereas in others, uppercase and lowercase letters will be
treated as different.
This is the last filter in that pipeline I showed you earlier. The
uniq command removes a line from a file if it is identical to
the one preceding it.
If you sort a file that has duplicate lines, and pipe it through
uniq, the duplicates will be removed.
You could do the same thing by using the -u option with
sort though, so this is not why uniq is unique.
Sometimes there are files that are not sorted but have "runs"
of the same lines. You could sort them using sort -u, but it
is fast to just run uniq on them.
For example
$ who | wc -l
2
displays the number of users currently logged in, and
$ ps -ef | grep bash | wc -l
displays how many people are running bash at the moment.
Read the man pages for sort, fold, and wc. Familarize
yourself with them because they are very useful.
Read about cut; it is also useful.
There is a file in the cs132/data directory named
directors. Sort it by movie title, then by director last
name. Print out just the movie titles. Print out just the
director names.
Download any spreadsheet and save as a CSV (comma
separated values) file, using tabs to separate the fields.
Experiment with sort to see how you can sort this file in
different ways.