Donation?

Harley Hahn
Home Page

Send a Message
to Harley


A Personal Note
from Harley Hahn

Unix Book
Home Page

SEARCH

List of Chapters

Table of Contents

List of Figures

Chapters...
   1   2   3
   4   5   6
   7   8   9
  10  11  12
  13  14  15
  16  17  18
  19  20  21
  22  23  24
  25  26

Glossary

Appendixes...
  A  B  C
  D  E  F
  G  H

Command
Summary...

• Alphabetical
• By category

Unix-Linux
Timeline

Internet
Resources

Errors and
Corrections

Endorsements


INSTRUCTOR
AND STUDENT
MATERIAL...

Home Page
& Overview

Exercises
& Answers

The Unix Model
Curriculum &
Course Outlines

PowerPoint Files
for Teachers

Exercises and Answers for Chapter 18...

Filters: Counting and Formatting

Review Question #1:

What are the three principal options of the wc program, and what does each one do?

What is it about wc that makes it such a useful tool?

When you use wc, what is the definition of a "line"?

Answer

-c counts characters
-w counts words
-l counts lines

The wc program is so useful because many types of data can be analyzed by counting lines and, to a lesser extent, counting words or characters. For example, it is common to use the grep program (Chapter 19) to select lines from a text file or data stream that meet a certain criteria. If you pipe such output to wc to count these lines, it will tell you how many lines in the original data meet the specified criteria.

When you use wc, a "line" is is a sequence of characters ending with a newline.

Review Question #2:

When you use tabs with Unix, what are the default tab positions?

Answer

By default, the spacing between tabs is 8 characters. Thus, the default Unix tab positions are 1, 9, 17, 25, 33, and so on.

Review Question #3:

The fold, fmt and pr programs can all be used to reformat text. What are principal differences between these programs?

Answer

fold works with lines
fmt works with paragraphs
pr works with pages and columns

Review Question #4:

The fold, fmt and pr programs have different default line lengths. What are they?

Answer

Default line lengths:

fold 80
fmt 75
pr 72

Applying Your Knowledge #1:

Use the command less /etc/passwd to look at the password file on your system. Notice that the file contains one line per userid, and that each line contains a number of fields, separated by : characters. The first field is the userid. Create a pipeline that generates a sorted, numbered list of all the userids on your system. Hint: use cut (Chapter 17), then sort, then nl.

Answer

cut -d':' -f1 /etc/passwd | sort | nl

Applying Your Knowledge #2:

The command ls displays a list of all the files in your working directory (except dotfiles). Create a pipeline that counts the number of files. Hint: use ls, then wc with the appropriate option.

Next, create a command that displays output like the following (where xx is the number of files):

I have xx files in my working directory.

Hint: Use echo (Chapter 12) with command substitution (Chapter 13), making use of the pipeline you just created.

Answer

To count the number of files:

ls | wc -l

To display the informative comment:

echo "I have "`ls | wc -l`" files in my working directory."

Applying Your Knowledge #3:

Go to a Web site of your choice, and copy some text into the clipboard. From the Unix command line, use the cat program to create a file named webtext:

cat > webtext

Paste in the text and press ^D. (Copy and paste is discussed in Chapter 6.) You now have a file containing the text from the Web site.

Create a pipeline that formats this text into 40 character lines, changing multiple spaces to single spaces. At the end of the pipeline, display the text one screenful at a time.

Answer

If you are not sure where to find text to copy, try one of the following pages:

https://www.harley.com/art/
https://www.harley.com/money/

To format the text:

fmt webtext -u -w 40 | less

Applying Your Knowledge #4:

Using the webtext file from the last example, format the text into pages with two columns, suitable for printing. Each column should be 30 characters wide, and the pages should be 20 lines long. The columns should be created with spaces, not tabs. Display the formatted output one screenful at a time.

Once you are satisfied that the output is correct, save it to a file named columns.

Answer

To create and display the formatted output:

fmt -w 30 webtext | pr -2 -l 20 -w 62 | expand | less

To save create and save the formatted output:

fmt -w 30 webtext | pr -2 -l 20 -w 62 | expand > columns

Note: The width used with pr is 62 because there are two 30-character columns, plus one space extra per column. The expand program is used to change tabs to spaces.

For Further Thought #1:

As we discussed in the chapter, 80-column lines were used widely in the world of computing because, in the 1950s, the first IBM computers used punch cards, which could hold 80 characters per card. The number 80 was mere serendipity, as the size of the punch card was taken from the size of the old U.S. dollar bill. This is an example of how old technology influences new technology.

In a similar manner, when IBM introduced the PC in 1981, the keyboard design was based on the standard typewriter, which used the so-called QWERTY layout (named after the six keys at the top left). It is widely accepted that the QWERTY layout is a poor one, as the most important keys are in particularly awkward locations. Although there exist much better keyboard layouts, the QWERTY keyboard is still the standard.

Why do you think old technology has such a strong influence on new technology?

Why is this bad? Why is this good?

(Take a moment to look up the layout of the Dvorak keyboard, an intelligent alternative to the QWERTY. I have been using a Dvorak keyboard for years, and I would never switch back.)

Answer

Old technology has a strong influence on new technology for several reasons:

• At any time, there are many existing tools and techniques associated with the old technology. In general, it is expensive and frustrating to implement brand new technology on a large scale.

• Once people get used to something, they don't like to change. Thus, a change to new technology is often difficult to achieve unless there is a clear and compelling payoff. For instance, you will often hear ignorant or anxiety-prone people make the statement: "It it isn't broken, don't fix it."

• New technologies are rarely invented anew. In most cases, they are variations of an older, existing technology.

• New technology costs money for purchases, retraining, and (often) other upgrades to make the new technology work properly with existing resources. In most cases, such money is difficult to procure, especially in a business environment.

The fact that old technology has a strong influence on new technology is bad because it stifles innovation, and cause people to work with inferior tools for much longer than necessary, sometimes indefinitely (for example, the QWERTY keyboard).

However, this influence can also be good, as it ensures that change does not happen so rapidly that people have trouble adjusting. It also keeps misguided people from rushing it embrace one new technology after another for no other reason than novelty.

Jump to top of page