Lab #5
CS 2351
UNIX Operating System

Name _________________________________

Section ___________________

This lab will use the following commands: head and tail, cut and paste and spell will be covered. The find and bc will also be covered. 

Make a new directory named lab5. Change to lab5. Type in:

                pwd

What was the answer?______________________________________________________________

Cut Command

Back in the lab4 directory,  you copied a file called employee to that directory. Copy that file to this directory by typing in:

               cp ../lab4/employee employee

Note that in this case, you had to type in ".." before you typed in the directory. The two periods put you back to your home directory and then you went to lab4.

Sometimes you only want to list part of a file. One example would be the dates of employment or just the departments. Instead of listing the entire file, you may just take one part of it. If you wanted to list just departments, you can use the cut command to do this.  The basic format for this is

                cut   -clist  filename

The -c option specifies that you want to cut out specific character positions from each line of a file. The specified files list tells which character positions to cut. To list only the departments, type in the following.

                cut -c1-3 employee

Be sure that you type in a number 1 after the -c in this example. If you type in the letter "l",  you will get an error message. You would then get a list of the departments only. You can also choose to list from a certain character to the end of the line.  Type in the following:

                cut -c5- employee

This would give you the names of the employees and all other information on them.
Obviously, you need to count the characters of the fields in order to make sure you
have the characters that you want to include. You can also list two fields that are not next
to each other.

Type in the following:

               cut -c1-3,31-37 employee

What was the results?
________________________________________________________________________________

________________________________________________________________________________

________________________________________________________________________________

This will list out the first field on department and also salary. Unfortunately, there are no spaces between the fields when they are listed. One solution would be to type in a space between the two. Type in:

                cut -c1-4,31-37 employee

What happened when you did this?____________________________________________________

______________________________________________________________________________

______________________________________________________________________________

Try some other examples. Use the cut command to list out only the names in the file employee. Hint, the names are in columns 5 to 21.  What command did you use?

___________________________________________________________________________

Try one more cut example with the employee file. Use two fields, name (5-21), and salary (31-37).  What command did you use?

___________________________________________________________________________

Next, type in:

                date

What is the result. List what spaces the month covers._________________________________

Try the following command:

                cut -c5-7 date

What happened when you did this?
______________________________________________________________________________

Try typing in the following:

                date | cut -c5-7

What happened this time?
_____________________________________________________________________________

Why do you think that it worked the second time and not the first time?

____________________________________________________________________________

You can use other commands with the cut.  In this next example, you could  sort the file employee and then pass it on to the cut command by using the pipe command. Type in the following:

                sort -n +4 employee | cut -c31-37

What was printed out?

__________________________________________________________

Try one more example using uniq, with the finger and cut command. Many times when you use the finger command, there are names of people who appear on the list several times. These people are using the x-windows features. Every time they have a different window, it looks like they are logged on another time. Try the following command:

        finger | cut -c9-28 | sort | uniq -c > lists

After you have completed this, use the cat command to look at the lists file. What does the lists file contain?

_______________________________________________________________________________

_______________________________________________________________________________

_______________________________________________________________________________

If you want to change the order of the fields in a file, you can do this by using the cut and paste command. The paste command is discussed next.

Paste Command

The paste command combines columns of data. It gives you some flexibility. You can combine several files--each of which has a single column of data--into a large table. You can also combine consecutive lines of data to build multiple columns. The paste command is similar to this:

                paste [ -d char] filename

The "char" is a character to be used as a separator, and filename is the name of the input file. You use paste to combine columns of data into one large table. In order to see how paste works, first use the cut command to cut columns out of the employee file. Do the following:

cut -c1-3 employee > dept
cut -c5-2
1 employee > name
cut -c31-37 employee > salary

You will now have three new files, dept, name, and salary. You can now combine them in a different order by using the paste command. Type in the following:

                paste dept salary name > employ2

List out the file, employ2. What does the file contain? ___________________________________

____________________________________________________________________________

____________________________________________________________________________

Notice that there are large spaces between the columns. This is because paste puts a tab character between each column. UNIX assumes tabs are set every 8 positions, starting with position 1. This means that tabs are set at 1, 9, 17, 25, and so on. If you would like paste to use a different character between columns, use the -d (delimiter) option followed by an alternative character in single quotes. To create a table with a space between columns, type in:

                paste -d' ' dept salary name > employ3

List out the file, employ3. What is the difference between the file employ2 and employ3?

______________________________________________ ______________________________

_____________________________________________________________________________

_____________________________________________________________________________

Head command

If you have a long file and you want to only list part of it, you can use the head command. By default, head will list the first 10 lines of a file. You also have the option of choosing how many lines you want to see. To see how this command works, copy the students file to your directory. Type in the following:

                cp /pub/htdocs/newuser/students .

The file contains names, id numbers, class, sex, earned hours, grade point average, birthday, and starting date of when student started school. This is all made up data. Find out how many lines there are in the file. What command do you use to do this and how many lines long is the file. (hint: use the "word count" command).

_____________________________________________________________________________

Next, type in:

                head students

What happened when you did this? __________________________________________________

_____________________________________________________________________________

You can ask to see more than the first 10 lines. The following is an example on how to show a certain number of lines you want to see.  Next, try:

                head -16 students

What was the results? ____________________________________________________________

_____________________________________________________________________________

Head shows the beginning of a file. If you want to see the last part of a file, you can use the tail command.

Tail Command

As with the head command, the default for tail is the last 10 lines of a file. You can also start displaying at a particular line, you can use either + or -, followed by a number. If you use the +, tail counts from the beginning of the file. If you use the -, tail counts from the end of the file. Try the following to see how it works.

                tail students

What was printed out? ___________________________________________________________

_____________________________________________________________________________

_____________________________________________________________________________

Try this example:

                tail -15 students

What was the result of this output? _________________________________________________

____________________________________________________________________________

Next, try the following:

                tail +20 students

This particular command should have displayed the file students from line 20 to the end. The tail command is one of the only commands in UNIX that uses a + to indicate an option. Usually, the - is the only delimiter for options that are available.

Spell filter 

The spell filter checks for spelling errors in a file.  In order to see how this works, use the vi editor to type in file1.  It contains the following:

        potatoe
        banana            
        aple
        turkey 

After you have completed it, type in:

                            spell file1

What was the result? 

__________________________________________________________________________

__________________________________________________________________________

Bc command

The bc command is a calculator that you can use on UNIX. It is a fully programmable mathematical interpreter. You will be using it for some calculations in the next part of the lab.  To see how this works, type in:

                bc

Once you start bc there is no specific prompt. You will just get a blank line. You just enter one calculation after another. Each time you press the <Enter> key, bc evaluates what you have typed in and displays the answer. Type in the following:

                12245 + 60015 - 4356 <Enter>

What was the answer? _________________________________________________

Try another one. Type in:  10*2/3

What number did you get? ______________________________________________

Although this number should have had some numbers after the decimal point, bc assumes that you are working with whole numbers. That is, it will not keep any digits to the right of the decimal point.  If you want fractional values, you need to set a scale factor to tell bc how many digits you want to keep to the right of the decimal. Type in the following:

                scale=3

Be sure that you do not leave any spaces before or after the "=" sign. From now on, all work will be done with three decimal places. Any extra digits will be truncated. If you want to check the scale factor, type in:

                scale

Now that you have the scale factor added, type in the following number again:

                10*2/3

What answer did you get? ______________________________________________

Bc follows the general rules of algebra. These are listed in priority order from highest to lowest:

Parentheses () The expression in the () is evaluated first. Used to override the defualt precedence of other operators.
Exponentiation ^ The value on the left is raised to the power on the right (example : 3^4 is 81)
Square Root sqrt (x) Returns the square root of x
Multiplication * Returns the product of the values on the left and right.
Division / Real division with the resulting digits to the right of the decimal set by the scale function
Modulo % Modulus division. Performs an integer division of the term on the left by the term on the right and returns the remainder
(example :  53%10 is 3)
Addition +
Subtraction -

Try the following to see the differences in order. Type in:

                1+2*3

What was the answer? ________________________________

Now try the number with parentheses around part of it. Type in:

                (1+2)*3

What was the answer?_________________________________

When you are finished working with bc, stop the program by pressing the "Ctrl  d" keys together. 

At this time, press the two keys: 

                                         Ctrl d  

You should now be returned to your $ prompt.

Find Command

Sometimes you have a file and you cannot remember which directory you have put it in. The find command can be used to obtain the full pathname to that file. The find utility selects files that are located in specified directories and are described by an expression. It does not generate any output unless you explicitly ask for it. The expression is:

                find directory-list expression

Two options that can be used are:

-name filename The file being evaluated meets this criterion if filename  matches its name.
-print The file being evaluated always meets this action criterion. When evaluation of the expression reaches this criterion, find displays the pathname of the file it is evaluating. NOTE: On some machines, you won't get any output from the find command unless you have the -print command attached to the command.

The find command works from the current working directory and on down to all of your subdirectories. If you wish to search all directories, you should be in your home directory.

Change back to your home directory by typing in:

                cd

Make sure that you are in your home directory at this time by typing in pwd. Type in the following command:

                find . -name 'file*'

What were the files that you saw listed? _____________________________________________

___________________________________________________________________________

___________________________________________________________________________

On this particular machine, you do not need the "-print" to get the results from the find command.

Notice that you can use wildcards (*), if there is more than one file with the first of that name, file. Also notice that the . (period) was used after the find command. The command uses the period to designate the working directory. NOTE: there is a space before and after the period. If you do not use the ., you will get an error message.  You do not need to use the quotes around the name of the file, if it is only one file.  If you use the * or ?, you need the single quotes around the name. 

Now use the find command to find the file, employee.

What commands did you use?_________________________________________________

List the pathname(s) listed for the file(s) employee:

 _____________________________________________________________

___________________________________________________________________________

At the end of this lab, you should have the following files in the directory, lab5.


employee   employ2    employ3   lists
   file1 
dept    name       salary        students

DO NOT REMOVE ANY OF THE FILES FROM THIS DIRECTORY. You will be referring to them again in some later labs.

Please indicate any parts of this lab that were hard to understand.

_______________________________________________________________________

_______________________________________________________________________

05/21/03