Sei sulla pagina 1di 2

Text processing tool

Scenario:

Devise, then write down the solution to each exercise. Remember, the answer is
the command you devise, not its output! The Answers are listed in the Solutions
section at the end of this lab, but you should try to work each task out on your
own before checking the answers. The key is to remember which tool is
appropriate for which job:
less for viewing text one page at a time
grep for extracting lines of text
cut for extracting columns of text
wc for counting lines, character, etc
sort for re-arranging lines
uniq for counting and/or removing adjacent duplicates
When faced with a problem that requires more than one of these abilities, simply
connect the appropriate tools together in a pipeline!

Instructions:
1. Other than the man page, how could you get a summary of options for the aspell command,
displayed one page at a time?
-------------------------------------------------------------------------------------------------------------------2. How many files are in the directory /usr/bin? The output should be a single integer.
HINT: devise a command that lists the filenames one per line, then think about how you can
count those lines.
-------------------------------------------------------------------------------------------------------------------3. List the misspelled words in the /usr/share/doc/HTML/index.html file.
-------------------------------------------------------------------------------------------------------------------4. How many times does each misspelled word appear?
HINT: Do you remember a technique for counting adjacent duplicate values?
-------------------------------------------------------------------------------------------------------------------5. Display the /etc/passwd line for any account that starts with the letter g
-------------------------------------------------------------------------------------------------------------------6. Display the number of lines in /etc/passwd
-------------------------------------------------------------------------------------------------------------------7. Display a list of usernames (and no other data) from /etc/passwd
-------------------------------------------------------------------------------------------------------------------8. Display the /etc/passwd line for any account that is using the bash shell
-------------------------------------------------------------------------------------------------------------------9. Display the /etc/passwd line for any account that is not using the bash shell
--------------------------------------------------------------------------------------------------------------------

10. Display a list of files in /etc/ that contain the word root. Display only the filenames and do
not print errors
HINT: one of these is done with an option (search the relevant command's man page!), the other
is done with output redirection.
-------------------------------------------------------------------------------------------------------------------11. Display the shell being used by root, as stored in /etc/passwd. Print only the name of the
shell and no other information. Be careful not to match more lines than you intend to!

Sequence 2:
Scenario:

Stream editing with regular expressions

Consider a file called 'cats' containing the following list of words:


cat
catalog
concatenate
polecat
Cat
For each sed command below, enter the new text for each line of the file. Enter
'No Change' if the line would be unaffected.

Instructions:
1. sed 's/cat/dog/' cats
cat
catalog
concatenate ..
polecat
Cat ..
2. sed 's/^[Cc]at/dog/' cats
cat ..
catalog
concatenate
polecat
Cat .
3. sed 's/^cat$/dog/i' cats
cat ..
catalog
concatenate
polecat
Cat..

Potrebbero piacerti anche