Lab 5 - Unix Lab#5 http://cs.okstate.edu/~c5235...

Info iconThis preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 4
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 6
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 8
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Unix Lab#5 http://cs.okstate.edu/~c5235 l/unixOShtml ’0/(0 Lab #5 CS 2351 UNIX Operating System Name if i 7 Section 1—}0 Z. This lab will use the following commands: head and tail, cut and paste and spell will be covered. The find and be will also be covered. Make a new directory named lab5. Change to lab5. Type in: pwd What was the answer? 4am: 6c 1 [a b S / 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 out. To list only the departments, type in the following. cut —cl —3 employee Be sure that you type in a number 1 after the -c in this example. If you type in the letter "1", 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. 10f8 2/17/2011 12:30 PM Unix Lab#5 http://cs.okstate.edu/~csZ351/unix05.htrn1 Type in the following: cut -cl-3 , 31—37 employee What was the results? I 3 ML time is anal mam; New R i 0 ate 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 :3 ‘ Mm .,:§"L Z343 “twee iii“ mi Ami Mifllgs / akin"; What happened when you did this? A 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? (14% '“CS'ZI /€mVP/0}/<§.— Try one more out example with the employee file. Use two fields, name (5-21), and salary (3 1-3 7). What command did you use? Cw; “(5'22 3i”3 7 .9 Migié’yté’ Next, type in: / date What is the result. List what spaces the month covers. 5 T 7 Try the following command: cut —c5—7 date What happeged when you did this? /" I , g 696 1 na «Staci-i ,5; o/ fifil/glfiygr‘y Try typing in the following: date I cut -c5-7 What happened this time? El; 2 of8 2/17/2011 12:30 PM Unix Lab#5 http://cs.okstate.edu/~cs2351/unix05.htrn1 Why do you think that it worked the second time and not the first time? .i‘i ca//¢A_mf_;l<£/t ‘H‘Lfi p545“! :35 ,“i 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: {am-manta! sort —n —k5 employee I cut —c31—3'7 What was printed out? If you want to change the order of the fields in a file, you can do this by using the cuta/nd 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—21 employee > name out -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 Look at the file, employ2. Describe the contents. 1 la kgjivs mg a,“ flit! «2 p l A we? . Mg, r . g. s. V . - {f i , ,' V‘ ,, an fig {5}”: i , 3 é"; 1, if: bipgfr ,r 1 «L133: 5* 5m y . r if: I / ~ 3 25M" «er-1‘- ~:3»; if; 33 A,M~‘~. 3, Notice that there arellarge 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 Look at the file, employ3. What is the difference between the file employ2 and employ3? 3 of8 2/17/2011 12:30 PM Unix Lab#5 http://cs.okstate.edu/~c5235 l/unix05.html ibis” i5» 9/119“? £32m... abs rm“; (W34 iii-‘95:“; 9:“ 8 Si 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. W!— ‘l m5!me ——/ Next, type in: head students What happened when you did this? {35% were 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? TL J m4- _L(_, pgmfiifi «é 9L Wag, Wig; 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 m What happened? last 7 .. 4of8 2/17/2011 12:30 PM Unix Lab#5 http://cs.0kstate.edu/~cs2351/unix05.html Try this example: tail -15 students What was the result of this output? Tit/L ins? 95 ii“: N g, m A wig / 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 filel. It contains the following: potatoe banana aple turkey After you have completed it, type in: spell filel What happened? Tim , die “ ml Hwiaiw H was; iii 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: be Once you start be 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: 12345 + 23456 — 7891 <Enter> What was the answer? 27‘? i a Try another one. Type in: 5*5/ 2 / What number did you get? I 2. Although this number should have had some numbers after the decimal point, bc assumes that you are 5 of8 2/17/2011 12:30 PM Unix Lab#5 http://cs.okstate.edu/~c52351/unix05.html 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 be how many digits you want to keep to the right of the decimal. Type in the following: scale=3 "—1! 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: 5*5/3 What answer did you get? 8. ‘3: ‘3 3 // 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 : 3A4 is 81) Square Root sqrt (2:) 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? 7 Now try the number with parentheses around part of it. Type in: (1+2) *3 6 of8 2/17/2011 12:30 PM Unix Lab#5 http://cs.0kstate.edu/~c5235 1/unix05.html What was the answer? Q When you are finished working with be, 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: The file being evaluated meets this criterion if filename —name f ilename . matches its name. 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. -print 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: ad Make sure that you are in your home directory at this time by typing in pwd. Type in the following command: find . —name 'file*' - ‘ ~.i "t. ' x i What were the files that you saw listed? with “g; :v ‘ ‘3'. E,» 1- 'V V ., .32 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. 7of8 2/17/2011 12:30 PM Unix Lab#5 http://cs.okstate.edu/~cs2351/unix05.html 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. I V \ What commands did you use? 'Pwi _. "‘ 15 area {impinge e i E List the pathname(s) listed for the file(s) employee: [flog/em. - 7 / ’/3M&ln¥£ 5 At the end of this lab, you should have the following files in the directory, labS. employeew employ2 1/ employ3V f i lelV deptv name « salary« students v‘ 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. A/[A Fa1107 8 of8 2/17/2011 12:30 PM ...
View Full Document

Page1 / 8

Lab 5 - Unix Lab#5 http://cs.okstate.edu/~c5235...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online