User Tools

Site Tools


haas:spring2014:unix:labs:laba

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
haas:spring2014:unix:labs:laba [2013/11/06 17:45] – external edit 127.0.0.1haas:spring2014:unix:labs:laba [2014/03/23 19:10] (current) wedge
Line 1: Line 1:
-<WRAP round box> +<WRAP centeralign round box> 
-\\  +<WRAP><color red><fs 200%>Corning Community College</fs></color></WRAP> 
-<WRAP centeralign bigger><WRAP bigger fgred>Corning Community College</WRAP> +<WRAP><fs 150%>CSCS1730 UNIX/Linux Fundamentals</fs></WRAP> 
-\\  +<fs 125%>Lab 0xA: Filters</fs>
-<WRAP muchbigger>UNIX/Linux Fundamentals</WRAP>  +
-\\  +
-\\  +
-Lab 0xA: Data Analysis with Regular Expressions and Scripting +
-\\  +
-\\  +
-</WRAP>+
 </WRAP> </WRAP>
 +
 ~~TOC~~ ~~TOC~~
 =====Objective===== =====Objective=====
-To continue to build on our knowledge of Regular Expressions, Scripting, and utilizing them to solve problems. +To become familar with the concepts of text filtering, and some of the UNIX utilities that are useful in this process.
 =====Reading===== =====Reading=====
-Referencing the following manual pages will likely be of considerable value:+Check out the manual pages for the following utilities:
  
-  * **grep**(**1**) +  * **cat**(**1**) - concatenate files 
-  * **sed**(**1**) +  * **cut**(**1**) - cut text 
-  * **awk**(**1**)+  * **grep**(**1**) - globally search for regular expression and print 
 +  * **head**(**1**) - print first "n" lines of output 
 +  * **sed**(**1**) - stream editor 
 +  * **sort**(**1**) - sort output 
 +  * **tail**(**1**) - print last "n" lines of output 
 +  * **tr**(**1**) - translate characters 
 +  * **uniq**(**1**) - filter out duplicate lines from sorted file 
 +  * **wc**(**1**) - word count
  
-=====Note===== +In "Harley Hahn's Guide to UNIX and Linux", please read:
-This lab is involved. Information obtained in early steps are built upon with increasingly complex functionality. +
- +
-If at any point something doesn't make sense, or you aren't getting the output you think you should be getting- **ask**. +
- +
-It is your responsibility to understand what is going on, please be proactive by asking questions (mailing list, IRC, etc.).+
  
 +  * Chapter 16 ("Filters: Introduction and Basic Operations", pages 373-394).
 +  * Chapter 17 ("Filters: Comparing and Extracting", pages 395-420).
 +  * Chapter 18 ("Filters: Counting and Formatting", pages 421-446).
 +  * Chapter 19 ("Filters: Selecting, Sorting, Combining, and Changing", pages 447-496).
 =====Background===== =====Background=====
 +Filtering is a big deal in many areas that deal with information processing. Say you've got a database of produce for a grocery store, and you want to view JUST the information regarding the banana shipments.. instead of sorting through the entire database and picking out the data you want manually- why not put all the data through a filter and simply view the pertinent data?
  
-As we've been exploring Regular ExpressionsShell Scripting, and even the various tools on the system, you've been told that these are important building blocks to aid you in more effective problem solving.+<WRAP round info box>A filteras defined on http://dictionary.reference.com/ is a program or routine that blocks access to data that meet a particular criterion.\\ 
 +\\  
 +For example: a Web filter that screens out vulgar sites. 
 +</WRAP>
  
-Now, we've amassed a considerable amount of knowledgewe can begin to see the fruits of our laborand will put our skills to use solving a problem that might previously have been considered "difficult" or near "impossible".+UNIX provides some utilities that allow you to accomplish impressive amounts of filtering. When coupled with Regular Expressionsyou can combine the power of pattern matching with your filteradding considerable flexibility to your arsenal of tricks.
  
-=====Problem Description===== +The next step is to apply shell scriptswhich allow you to write "programs" that can take advantage of all the utilities and features available on a systemWe will be looking at shell scripts another week. First we must get the foundations in place so we can better appreciate shell scripts.
-As students at CCCa routine activity that takes place each semester is the selection of classes for the following semesterTo put a schedule together, courses must be looked up and a selection of compatible times are selected, sometimes choosing from a selection of offerings, and ultimately a CRN (Course Reference Number) must be identified in order to communicate to the system.+
  
-On the main [[http://www.corning-cc.edu/|CCC web site]] is a link entitled [[https://ssbprod.corning-cc.edu/PROD/bwckschd.p_disp_dyn_sched|Class Schedule]], which allows us to select a semester and perform searches for available courses.+So, some basics of filtering:
  
-As it turns outthis functionality generates data in HTML format, yet it contains all the useful information we might possibly need for course selections.+In order to do any sort of filtering, we need to know what we want to filter. Makes sense.
  
-This is one of those perfect examples that can be solved with our UNIX skills... the data we find available to us is in a form not immediately readable for our needs.. so what do we do when the universe doesn't align to our needs? We realign the universe!  +Before we employ filtering, we must have some clear idea about what we would like to filterand how to safely maintain the data we wish to let through(Filtering is no good if the data you are after gets damaged in the process).
-=====Obtain the Data===== +
-In preparation for this exerciseI have taken the liberty of downloading a class listing for the Fall 2013 semesterThis list contains all the courses offered at the primary college locations and Internet courses, and excludes ACE courses, and courses taught at non-primary college locations (high schools, etc.).+
  
-This file can be found in the **courselist/** subdirectory of the UNIX Public DirectoryIt is called: **fall2013-20130417.html.gz**+The UNIX **cat**(**1**) utility is a general all-purpose tool that can be used to display the contents of text files. **cat**(**1**) also provides a number of other features that can be handy for debugging problems you may encounter with text files(the **-n** and **-e** arguments can be particularly useful).
  
-We'll want to copy this file to your home directory.+Let's play with a sample database. In the **filters/** subdirectory of the UNIX Public Directory you will find a file called "**sample.db**". Copy it to your home directory.
  
-^  1 ^|Do the following:+Let's try some stuff out.
-| ^  a.|Copy the indicated file to your home directory. How did you do this?| +
-|:::^  b.|List the file. How large is it?| +
-|:::^  c.|What type of file is it? How did you determine this?| +
-|:::^  d.|We want the HTML data, so unravel this file to obtain that data. How did you do this?| +
-|:::^  e.|How large is the HTML data?| +
-|:::^  f.|What is the compression ratio achieved with this data?|+
  
-Now that we have a copy of the datawe can move on to studying it.+====No filteringor a filter that lets everything through====
  
-=====Analyzing the Raw Data===== +Display the contents:
-The first step we must take when tackling a problem like this is to get an understanding of the data we are working with. Regular Expressions are cool and all, but they aren't useful unless we know what it is we are describing.+
  
-Our first task is to locate any common patterns in our data that we might be able to use to our advantage with Regular Expressions.+<cli> 
 +lab46:~$ cat sample.db 
 +</cli>
  
-^  2.  ^|Viewing the HTML file in **vi**, answer me the following:+This is the simplest form of filtering possible-- none at allAll the data in the text file is passed to **STDOUT**.
-| ^  a.|This file contains courses offered next semester. Search for the course entry for "CSIT 2044"How did you do this?| +
-|:::^  b.|Comparing the data in this fileis there any similarity to an "ENGL 1020" course? How about a "MATH 1230" course? Is there any pattern in common among all the courses?|+
  
-As it stands, each course has an information string as follows (I'll use UNIX as an example):+Even at this stage we can do some useful things with the data. For example, if we wanted to find out how many lines were in the database:
  
-^  UNIX/Linux Fundamentals - 92629 - CSCS 1730 - 001  | +<cli
- +lab46:~$ cat sample.db | wc -l 
-After the initial HTML data, we get actually course data we are interested in... there's a pattern we can take advantage of here: the course information is separated into fields, and each field is separated by a hyphen "-". In its default state, the data is arranged as follows: +</cli>
- +
-  - Course Title +
-  - Course Reference Number (CRN) +
-  - Course Prefix/Number +
-  - Course Section +
- +
-Check out some other courses and verify that this pattern holds true. The actual data will vary, but the pattern/presentation of the data should be identical. Because of that, we can describe it with a Regular Expression pattern, and perform manipulations on it. +
- +
-=====Isolating the Course Information Strings===== +
-Although there's additional information (room, time, instructor, credits), let's start off by isolating all the individual course information strings (that exhibit that pattern above). +
- +
-Using the UNIX class again as an example, the actual line in question is as follows: +
- +
-<code html+
-<th class="ddtitle" scope="colgroup"><a href="https://bssprod.corning-cc.edu/PROD/bwckschd.p_disp_detail_sched?term_in=201410&amp;crn_in=92629">UNIX/Linux Fundamentals - 92629 - CSCS 1730 - 001</a></th> +
-</code> +
- +
-If we go and look at another class, say ARTS 1030, we see the following: +
- +
-<code html> +
-<th class="ddtitle" scope="colgroup"><a href="https://bssprod.corning-cc.edu/PROD/bwckschd.p_disp_detail_sched?term_in=201410&amp;crn_in=92630">Drawing I - 92630 - ARTS 1030 - 001</a></th> +
-</code> +
- +
-and GOVT 1010, we see the following: +
- +
-<code html> +
-<th class="ddtitle" scope="colgroup"><a href="https://bssprod.corning-cc.edu/PROD/bwckschd.p_disp_detail_sched?term_in=201410&amp;crn_in=93193">American Federal Government - 93193 - GOVT 1010 - 001</a></th> +
-</code>+
  
-In context, these lines are surrounded by other lines of information, which we aren't immediately interested in. For example, looking at HUMA 1020:+The database will display to STDOUT in all its entiretyYou will notice the database is setup as follows:
  
-<code html+<code> 
-</tbody></table> +name:sid:major:year:favorite candy
-<br> +
-<br> +
-</td> +
-</tr> +
-<tr> +
-<th class="ddtitle" scope="colgroup"><a href="https://bssprod.corning-cc.edu/PROD/bwckschd.p_disp_detail_sched?term_in=201410&amp;crn_in=92249">Basic Humanities II - 92249 - HUMA 1020 - 001</a></th>                                                                         +
-</tr> +
-<tr> +
-<td class="dddefault"> +
-<span class="fieldlabeltext">Associated Term</span>Fall 2013   +
-<br> +
-<span class="fieldlabeltext">Registration Dates</span>Mar 25, 2013 to Dec 15, 2013+
 </code> </code>
  
-Each class should be in a similar situation. The line containing the course information is surrounded by lines that contain other information (whether useful or useless, there is other data there than what we are presently interested in locating).+With this information we can make some important observations about the structure of the database:
  
-^  3.  ^|Through analyzing the data, answer me the following:+  * fields are separated by colon (:
-| ^  a.|If we wanted to perform a search that would only hit the course information lines (ie a pattern that would match just that line, and match that line for each course in the file), what does the RegEx pattern look like?| +  * last entry on the line is followed by a star (*)
-|:::^  b.|Perform the search in **vi** (using **/**, verify that it hits that line in some course). Does it snap to the appropriate line?| +
-|:::^  c.|Hit **n** to go to the next match. And hit **n** again. And again. Are you consistently hitting the course information line for each course?|+
  
-<WRAP round warning box>You absolutely need to have a correctly working pattern in order to proceedIf you have ANY questionsplease ask them. This lab will fail to cooperate with you if your pattern is not adequate.</WRAP>+To be effective in filtering text, we must be aware of the structure of that textThe more you know about how some structure is set upthe better we can design a solution to the particular problem. 
 +====keyword filtering====
  
-Return to the command prompt. Time to start prototyping our solution.+Ok, so let us filter some of this information:
  
-We'll want to come up with a command-line that isolates those course information lines for us. A prototype for that command-line will look something like this (substitute your working RegEx pattern in place of the string "REGEX" in the example below):+Find all the students who are in //Biology//:
  
 <cli> <cli>
-lab46:~$ cat fall2013-20110417.html | grep 'REGEX' | less+lab46:~$ cat sample.db | grep Biology
 </cli> </cli>
  
-When you put in the same pattern you came up with while searching in **vi**, your screen should be filled with data that looks like this (and much much more):+We can do more complicated searches too: 
 + 
 +Find all the students who are in Biology AND like Lollipops:
  
 <cli> <cli>
-<th class="ddtitle" scope="colgroup"><a href="https://bssprod.corning-cc.edu/PROD/bwckschd.p_disp_detail_sched?term_in=201410&amp;crn_in=92473">Accounting Practices - 92473 - ACCT 1000 - 001</a></th> +lab46:~$ cat sample.db | grep Biology | grep Lollipops
-<a href="https://bssprod.corning-cc.edu/PROD/bwckschd.p_disp_syllabus?term_in=201410&amp;crn_in=92473">Learning Outcomes</a> +
-<th class="ddtitle" scope="colgroup"><a href="https://bssprod.corning-cc.edu/PROD/bwckschd.p_disp_detail_sched?term_in=201410&amp;crn_in=92467">Accounting Practices - 92467 - ACCT 1000 - 002</a></th> +
-<a href="https://bssprod.corning-cc.edu/PROD/bwckschd.p_disp_syllabus?term_in=201410&amp;crn_in=92467">Learning Outcomes</a> +
-<th class="ddtitle" scope="colgroup"><a href="https://bssprod.corning-cc.edu/PROD/bwckschd.p_disp_detail_sched?term_in=201410&amp;crn_in=92938">Accounting Practices - 92938 - ACCT 1000 - 003</a></th> +
-<a href="https://bssprod.corning-cc.edu/PROD/bwckschd.p_disp_syllabus?term_in=201410&amp;crn_in=92938">Learning Outcomes</a> +
-<th class="ddtitle" scope="colgroup"><a href="https://bssprod.corning-cc.edu/PROD/bwckschd.p_disp_detail_sched?term_in=201410&amp;crn_in=92789">Accounting Practices - 92789 - ACCT 1000 - 005</a></th> +
-<a href="https://bssprod.corning-cc.edu/PROD/bwckschd.p_disp_syllabus?term_in=201410&amp;crn_in=92789">Learning Outcomes</a> +
-<th class="ddtitle" scope="colgroup"><a href="https://bssprod.corning-cc.edu/PROD/bwckschd.p_disp_detail_sched?term_in=201410&amp;crn_in=92792">Accounting Practices - 92792 - ACCT 1000 - 006</a></th> +
-<a href="https://bssprod.corning-cc.edu/PROD/bwckschd.p_disp_syllabus?term_in=201410&amp;crn_in=92792">Learning Outcomes</a> +
-<th class="ddtitle" scope="colgroup"><a href="https://bssprod.corning-cc.edu/PROD/bwckschd.p_disp_detail_sched?term_in=201410&amp;crn_in=92980">Accounting Practices - 92980 - ACCT 1000 - 007</a></th> +
-<a href="https://bssprod.corning-cc.edu/PROD/bwckschd.p_disp_syllabus?term_in=201410&amp;crn_in=92980">Learning Outcomes</a> +
-:+
 </cli> </cli>
  
-Because we piped our output to **less**(**1**), it stops after the first screenful of information. Pressing the down/up arrow keys or the space bar will navigate us through this data.+^  1.  ^|Perform the following searches on the database:
 +| ^  a.|Find all the students that are a //Freshman//
 +|:::^  b.|Same as above but in //alphabetical order//| 
 +|:::^  c.|Any duplicate entries? Remove any duplicates.| 
 +|:::^  d.|Using the **wc**(**1**) utilityhow many matches did you get?|
  
-What we're interested in at this point is that the data that is being produced all seems to match those lines in the file that contain the course information string+Be sure to give me the command-line incantations you came up with, and any observations you made
-=====Filtering unnecessary data===== +====filter for manipulation====
-When you're satisfied with the information your pattern and resultant **grep** search has produced, our next step is to refine the information-- to make it more readable.+
  
-To do this, we will make use of the **sed**(**1**) utility, which is a //steam editor//; it allows us to take the output and perform edits on it, much like we could in a text editor, only we specify on the command-line the actual work we wish to perform.+So we've done some simple searches on our database. We've filtered the output to get desired values. But we don't have to stop there. Not only can we filter the text, we can manipulate it to our liking.
  
-If you recall from our explorations of **vi**, it has a //search and replace// capability that proved to be rather powerful. **sed**(**1**) also possesses this ability, and we can unlock it as follows:+The **cut**(**1**) utility lets us literally cut columns from the output.
  
-<code bash> +It relies on a thing called a field-separator, which will be used as a logical separator of the data.
-cat FILE | grep 'REGEX' | sed 's/PATTERN/REPLACEMENT/g' | less +
-</code>+
  
-Of specific interest to us are the **s**, **PATTERN**, **REPLACEMENT** and **g** options to **sed**(**1**)They have the following functionality:+Using the "**-d**" argument to cutwe can specify the field separator in our data. The "**-f**" option will parse the text in fields based on the established field separator.
  
-  * **s**: Invoke the **sed**(**1**) //search and replace// command. By default the forward slash **/** is the field separator. +So, looking at the following text:
-  * **PATTERN**: The first field following the search command is the pattern we are looking for. In this case, we want to come up with a new pattern that will match a portion of the text we wish to get rid of. +
-  * **REPLACEMENT**This field is what we wish to replace any matched text with. +
-  * **g**: Not necessarily needed in all cases, the **g** indicates we wish to perform this search and replace globally for **all** occurrences on a line. I'd recommend getting in the habit of using it, and then recognizing when you don't want to use it.+
  
-So, looking at the data leading up to the course information we're interested in, can we come up with pattern to describe it? I think so.+<code> 
 +hello there:this:is:a:bunch of:text. 
 +</code>
  
-^  4.  ^|Craft a RegEx pattern that does the following:+Looking at this example, we can see that ":" would make for an excellent field separator.
-| ^  a.|Starts at the beginning of the line.| +
-|:::^  b.|Goes until it encounters some unique text just before our desired information.| +
-|:::^  c.|Specifically describe the pattern of the data just before our desired information.| +
-|:::^  d.|What is your pattern?|+
  
-To test your patternyou'll want to do the following:+With ":" as the field separator, the logical structure of the above text is logically represented as follows:
  
-<cli> +^  Field 1  ^  Field 2  ^  Field 3  ^  Field 4  ^  Field 5  ^  Field 6  | 
-lab46:~$ cat fall2013-20110417.html grep 'REGEX' sed 's/PATTERN//g' less + hello there   this  |  is  |  a  |  bunch of  |  text.  |
-</cli>+
  
-Where **PATTERN** is a new Regular Expression pattern that successfully matches the beginning of the lines we're interested in (actually all that **grep** is producing at this point), and replacing it with nothing (the two consecutive slashes indicate we're not interested in replacing the matched data with anything). 
  
-If successful your output should appear as follows (note this is showing fall 2011 data, so the CRNs and number of offerings may be different):+We can test these properties out by using **cut**(**1**on the command-line:
  
 <cli> <cli>
-Accounting Practices - 81559 - ACCT 1000 - 001</a></th> +lab46:~$ echo "hello there:this:is:a:bunch of:text." | cut -d":" -f#
-Accounting Practices - 82350 - ACCT 1000 - 003</a></th> +
-Financial Accounting - 82355 - ACCT 1030 - 001</a></th> +
-Financial Accounting - 81558 - ACCT 1030 - 002</a></th> +
-Financial Accounting - 81107 - ACCT 1030 - 003</a></th> +
-Financial Accounting - 81108 - ACCT 1030 - 004</a></th> +
-Financial Accounting - 81173 - ACCT 1030 - 005</a></th> +
-Financial Accounting - 82115 - ACCT 1030 - 006</a></th> +
-Managerial Accounting - 82078 - ACCT 1040 - 003</a></th> +
-Accounting Procedures - 81123 - ACCT 1050 - 001</a></th> +
-Federal Income Tax - 81783 - ACCT 1100 - 001</a></th> +
-Federal Income Tax - 82358 - ACCT 1100 - 002</a></th> +
-Intermediate Accounting I - 81124 - ACCT 2030 - 001</a></th> +
-Intermediate Accounting I - 82359 - ACCT 2030 - 002</a></th> +
-Computerized Accounting - 82361 - ACCT 2100 - 001</a></th> +
-Cultural Anthropology - 81139 - ANTH 2120 - 001</a></th> +
-Elem Mod Stand Arabic Con&amp;StrI - 82296 - ARAB 1010 - 001</a></th> +
-Elem Mod Arabic Con&amp;StrI - lab - 82297 - ARAB 1010 - 071</a></th> +
-Introduction Art Appreciation - 81505 - ARTS 1004 - 002</a></th> +
-Drawing I - 81771 - ARTS 1030 - 001</a></th> +
-Drawing I - 82176 - ARTS 1030 - 002</a></th> +
-Drawing I - 82112 - ARTS 1030 - 003</a></th> +
-Drawing I - 81503 - ARTS 1030 - 004</a></th> +
-Ceramics I - 81151 - ARTS 1210 - 001</a></th> +
-Ceramics I - 82110 - ARTS 1210 - 002</a></th> +
-Ceramics I - 81504 - ARTS 1210 - 003</a></th> +
-Ceramics I - 82134 - ARTS 1210 - 004</a></th> +
-Ceramics I - 81176 - ARTS 1210 - 005</a></th> +
-Basic Black &amp; White Photo - 81873 - ARTS 1220 - 001</a></th> +
-Basic Black &amp; White Photo - 81874 - ARTS 1220 - 002</a></th> +
-Basic Black &amp; White Photo - 81875 - ARTS 1220 - 003</a></th> +
-History/Appreciation of Art I 81180 ARTS 1310 - 001</a></th> +
-:+
 </cli> </cli>
  
-Our **sed** should have successfully stripped off the leading HTML text that we're uninterested in. Once that happenssuddenly our data becomes that much more readable.+Where # is a specific field or range of fields. (ie **-f2** or **-f2,4** or **-f1-3**)
  
-Note that there'still HTML data trailing our informationThat can be addressed in another **sed** call:+^  2.  ^|Let'play with the **cut**(**1**) utility:| 
 +| ^  a.|What would the following command-line display: **echo "hello there:this:is:a:bunch of:text." <nowiki>|</nowiki> cut -d":" -f3**
 +|:::^  b.|If you wanted to get "hello there text." to display to the screen, what manipulation to the text would you have to do?| 
 +|:::^  c.|Did your general attempt work? Is there extra information?|
  
-<cli> +If you found that extra information showed up when you tried to do that last parttaking closer look will show why:
-lab46:~$ cat fall2013-20110417.html | grep 'REGEX' | sed 's/PATTERN//g' | sed 's/<\/a>.*$//g' | less +
-Accounting Practices - 81559 - ACCT 1000 - 001 +
-Accounting Practices - 82350 - ACCT 1000 - 003 +
-Financial Accounting - 82355 - ACCT 1030 - 001 +
-Financial Accounting - 81558 - ACCT 1030 - 002 +
-Financial Accounting - 81107 - ACCT 1030 - 003 +
-Financial Accounting - 81108 - ACCT 1030 - 004 +
-Financial Accounting - 81173 - ACCT 1030 - 005 +
-Financial Accounting - 82115 - ACCT 1030 - 006 +
-Managerial Accounting - 82078 - ACCT 1040 - 003 +
-Accounting Procedures - 81123 - ACCT 1050 - 001 +
-Federal Income Tax - 81783 - ACCT 1100 - 001 +
-Federal Income Tax - 82358 - ACCT 1100 - 002 +
-Intermediate Accounting I - 81124 - ACCT 2030 - 001 +
-Intermediate Accounting I - 82359 - ACCT 2030 - 002 +
-Computerized Accounting - 82361 - ACCT 2100 - 001 +
-Cultural Anthropology - 81139 - ANTH 2120 - 001 +
-Elem Mod Stand Arabic Con&amp;StrI - 82296 - ARAB 1010 - 001 +
-Elem Mod Arabic Con&amp;StrI - lab - 82297 - ARAB 1010 - 071 +
-Introduction Art Appreciation - 81505 - ARTS 1004 - 002 +
-Drawing I - 81771 - ARTS 1030 - 001 +
-Drawing I - 82176 - ARTS 1030 - 002 +
-Drawing I - 82112 - ARTS 1030 - 003 +
-Drawing I - 81503 - ARTS 1030 - 004 +
-Ceramics I - 81151 - ARTS 1210 - 001 +
-Ceramics I - 82110 - ARTS 1210 - 002 +
-Ceramics I - 81504 - ARTS 1210 - 003 +
-Ceramics I - 82134 - ARTS 1210 - 004 +
-Ceramics I - 81176 - ARTS 1210 - 005 +
-Basic Black &amp; White Photo - 81873 - ARTS 1220 - 001 +
-Basic Black &amp; White Photo - 81874 - ARTS 1220 - 002 +
-Basic Black &amp; White Photo - 81875 - ARTS 1220 - 003 +
-History/Appreciation of Art I - 81180 - ARTS 1310 - 001 +
-</cli>+
  
-In the provided expression, the following happens:+If you tell **cut**(**1**) to display any fields that aren't immediately next to one anotherit will insert the field separator to indicate the separation.
  
-  * The pattern **<nowiki><\/A>.*$</nowiki>** explicitly matches the closing "**a**" tagand then matches whatever follows until the end of the line+So how do you keep this functionality while still getting the exact data you seek? Wellnobody said we could only apply one filter to text
-  * We replace that matched pattern with NOTHING.+=====The Stream Editor - sed=====
  
-<WRAP round info box>Note the presence of the backslash **<nowiki>\</nowiki>** before the closing slash of the **A** tag. This is needed because the forward slash **/** is the default field separator in **sed**(**1**), and to avoid the error of prematurely terminating the field, we use the backslash to escape it in order to match a literal forward slash.</WRAP> +Remember back when we played with **vi/vim**? Remember that useful search and replace command:
  
-The result should be as appears in the sample above... no HTML data, just real readable course information.+<code> 
 +:%s/regex/replacement/
 +</code>
  
-^  5 ^|Perform some data mining for me:| +That was quite usefulAnd luckilywe've got that same ability on the command lineIntroducing "**sed**(**1**)", the stream editor.
-| ^  a.|Of this listhow many courses is CCC offering next semester?+
-|:::^  b.|How did you produce this result?| +
-|:::^  c.|How many **CSCS** classes is CCC offering next semester? How did you find this?| +
-|:::^  d.|How did you produce this result?| +
-|:::^  e.|How many upper level (2000 and above) **ENGL** classes are being offered next semester?+
-|:::^  f.|How did you produce this result?| +
-|:::^  g.|Who is offering more courses next semester, the English or Math department?+
-|:::^  h.|How did you produce this result?|+
  
-Hopefully you're starting to see the value in what the Regular Expressions have enabled for us-- we were able to take raw data in some arbitrary formatand through analyzing itadequately whittle away at it until it becomes a format readable to us.+sed provides some of the features we've come to enjoy in vi, and is for all intents and purposes a non-interactive editor. One useful ability, however, is its ability to edit data streams (that is**STDOUT**including that generated from our command lines).
  
-Once in that formatwe can then perform some more valuable tasks on that data.+Perhaps the most immediately useful command found in sed will be its search and replacewhich is pretty much just like the **vi/vim** variant:
  
-=====Data Analysis===== +<code> 
-In the **courselist/** subdirectory of the UNIX Public Directory are some additional files of value:+sed -e 's/regex/replacement/g' 
 +</code>
  
-  * fall2010-20100315.html.gz +However, if you look close, you will see that we did not include any sort of file to operate onWhile we can, one of the other common uses of sed is to pop it in a command-line with everything else, stuck together with the all-powerful pipe (**|**).
-  * fall2010-20101113.html.gz +
-  * fall2011-20110417.html.gz +
-  fall2013-20130417.html.gz +
-  spring2010-20091022.html.gz +
-  spring2010-20101113.html.gz +
-  spring2011-20101105.html.hz +
-  * spring2011-20101113.html.gz +
-  * winter2011-20101113.html.gz+
  
-Each of these files contains a snapshot of semester course informationreferenced by semester, and snapshot date. Please make a copy of these additional files, uncompress them, and let's create a script to perform some data analysis.+For exampleso solve the above problem with the field separator:
  
-^  6.  ^|Write a script that does the following:| +<cli> 
-| ^  a.|Accepts 1 or more of these files as an argument.| +lab46:~$ echo "hello there:this:is:a:bunch of:text.cut -d":" -f1,sed -'s/:/ /g' 
-|:::^  b.|If no files are specified, display an error with usage information and exit.| +</cli>
-|:::^  c.|If one file is given, perform the logic we've done manually on the command-line to produce and display the total number of courses offered in the given semester's course file.| +
-|:::^  d.|If two files are givenand are both for the same semester+year, display the totals for each semester, and if the numbers do not match, display how both files differ (in an attempt to show what change took place).| +
-|:::^  e.|If two files are given, and are **not** the same semester+year, display the totals for each semester, and display how many English courses are being offered in each of the files.| +
-|:::^  f.|If more than two semesters are listed, do the same, but **also** display the totals for MATH, CSCS, BIOL, and PFIT.| +
-|:::^  g.|Provide a copy of your script.|+
  
-^  7.  ^|As you are playing with the different course data files:| +We used sed to replace any occurrence of the ":" with single space.
-| ^  a.|Comparing fall2011 to fall2013, which semester offered more courses?| +
-|:::^  b.|Do any of the files seem to break your logic?| +
-|:::^  c.|Which one(s)?| +
-|:::^  d.|Comparing a "working" file to "nonworking" one, what seems to be a difference that trips up your patterns?+
-|:::^  e.|Between which two snapshot dates did this change seem to take place?| +
-|:::^  f.|What can you surmise as being a cause of this change?| +
-|:::^  g.|Could you adapt your script to handle the two different formats of data? How would you do this?| +
-|:::^  h.|Provide a copy of your updated script.|+
  
-<WRAP round info box>__Hint:__ to compare differences between textual data setsexplore the **diff**(**1**) tool.</WRAP>+^  3.  ^|Answer me the following:
 +| ^  a.|Does the above command-line fix the problem from #2c?| 
 +|:::^  b.|If you wanted to change all "t"'s to uppercase "T"'s in addition to thatwhat would you do?| 
 +|:::^  c.|If you wanted to replace all the period symbols in the text with asterisks, how would you do it?| 
 +|:::^  d.|What does the resulting output look like?|
  
-There are a ton of questions we could ask of this data:+=====From head(1) to tail(1)=====
  
-  How many remedial (courses below the 1000 levelare offered a given semester? +Two other utilities you may want to become acquainted with are the **head**(**1**and **tail**(**1**) utilities.
-  Do any quantity of particular course(s) increase/decrease over time? +
-  Is there a noticeable change in certain course offerings between a fall and a spring?+
  
-Then there are some questions thatwith our current skill levelmay cause us a bit of trouble:+**head**(**1**) will allow you to print a specified number of lines from //1 to n//. So if you needed to printsaythe first 12 lines of a file, **head**(**1**) will be a good bet.
  
-  * What is the range of CRN numbers for a given semester? (Lowest through highest) +For example, to display the first 4 lines of our sample database:
-  * Which course prefix has the MOST offerings a given semester? +
-  * Which course prefix has the LEAST offerings a given semester? +
-  * Which course prefix offered the MOST remedial course offerings?+
  
-While we may be able to derive answers to these questions... in some respects the data is not conveniently arranged for our analysis purposes. At the moment we have our data in the following format:+<cli> 
 +lab46:~$ head -12 sample.db 
 +</cli>
  
-^  UNIX/Linux Fundamentals - 81769 - CSCS 1730 - 001  ^+And, of course, adding it onto an existing command line using the pipe. In this example, the first two results of all the *ology Majors:
  
-And to answer some of these questions, especially when **grep**'s are concerned, we'd ideally want the data arranged more like:+<cli> 
 +lab46:~$ cat sample.db | grep "ology" | head -2 
 +</cli>
  
-^  78400:CSCS 1730-001:UNIX/Linux Fundamentals  |+See where we're going with this? We can use these utilities to put together massively powerful command-line incantations create all sorts of interesting filters.
  
-So once again our data may not be exactly the way we want itDo we give up? **HECK NO**, we conform the universe to our demands...+**tail**(**1**) works in the opposite end- starting at the end of the file and working backwards towards the beginning. So if you wanted to display the last 8 lines of a file, for example. **tail**(**1**) also has the nifty ability to continually monitor a file and update its output should the source file changeThis is useful for monitoring log files that are continually updated. 
 +=====Translating characters with tr=====
  
-=====Rearranging Data with Regular Expressions===== +This is another useful tool to be familiar with. With **tr**(**1**), you can substitute any character or sequence of characters with anotherThe nice thing is that you can quickly use it to do end-of-line character translations, useful in converting text files from DOS format to UNIX or Mac format (or any combination therein). 
-I consider where we are at now to be amongst some of the most powerful of concepts we learn in this classWhat we are going to do now hopefully should take the cake and illustrate the true potential of the capabilities we are able to wield provided a good working knowledge of Regular Expressions and related tools.+====ASCII file line endings====
  
-To do our next trick, we need to study our data once again:+An important thing to be aware of is how the various systems terminate their lines. Check the following table:
  
-^  UNIX/Linux Fundamentals - 81769 - CSCS 1730 - 001  ^+^  System   Line Ending Character(s) 
 +|  DOS  |  Carriage Return, Line Feed (CRLF) 
 +|  Mac  |  Carriage Return (CR)  | 
 +|  UNIX  |  Line Feed (LF)  |
  
-As you can seethe information as it is currently formatted takes the following structure, as compared to the desired structure:+So what does this mean to you? Wellif you have a file that was formatted with Mac-style line endingsand you're trying to read that file on a UNIX system, you may notice that everything appears as a single line at the top of the screen. This is because the Mac uses just Carriage Return to terminate its lines, and UNIX uses just Line Feeds... so the two are drastically incompatible for standard text display reasons.
  
-^  Current:  |  Course Title - CRN - Course Prefix/Number - Section +For examplelet's say we have UNIX file we wish to convert to DOS formatWe would need to convert every terminating Line Feed to a Carriage Return & Line Feed combination (and take note that the Carriage Return needs to come first and then the Line Feed). We would do something that looks like this:
-^  Desired:  |  CRN:Course Prefix/Number-Section:Course Title  | +
- +
-So how could we do this? To accomplish this task, we need to explore another RegEx capability and exercise our options in the **sed** REPLACEMENT field. +
- +
-^  8.  ^|With our data in the current structure:+
-| ^  a.|Derive a RegEx pattern that will match up to the first "space dash space"What is your pattern?| +
-|:::^  b.|Derive a RegEx pattern that will match the CRN up to the second "space dash space". What is your pattern?| +
-|:::^  c.|Derive RegEx pattern that will match the Course Prefix/Number up to the third "space dash space". What is your pattern?| +
-|:::^  d.|Finally, round out with a fourth RegEx pattern that matches the **Section**, which is at the end of the line. What is your pattern?| +
- +
-For my examples, I'll name your patterns REGEX1, REGEX2, REGEX3, and REGEX4. +
- +
-In order to rearrange our data, we need to effectively describe the data (as you did abovein order to reference it in groupsThe RegEx symbols **\(** and **\)** denote Regular Expression groups, which we can use to isolate specific patterns for later reference. +
- +
-Check this out:+
  
 <cli> <cli>
-lab46:~$ cat fall2013-20110417.html | grep 'REGEX' | sed 's/PATTERN//g' | sed 's/<\/a>.*$//g' output +lab46:~$ tr "\n" "\r\n" < file.unix file.dos
-lab46:~$ +
 </cli> </cli>
  
-Notice what we just did here... we took our information in its current form of filtering and output it to a file (called **output**), effectively taking a snapshot of our progress.+To interpret this:
  
-That should make sense, we're just using **I/O Redirection** to send the output of that pipelined command-line to file instead of to **STDOUT**.+**\n** is the special escape sequence that we're all familiar with. In C, you can use it to issue an //end-of-line// character. So in UNIX, this represents Line Feed (**LF**).
  
-Feel free to make use of similar output junctures during the solution of a problem like this- and who knows, you might need to do particular processing with certain arrangements of data. So if you output your data at certain key points, you could be making your work lot easier.+**\r** is the special escape sequence that corresponds to a Carriage Return (**CR**).
  
-Moving on:+The first argument is the original sequence. The second is what we would like to replace it with. (in this case, replace every **LF** with a **CRLF** combination).
  
-<cli> +Then, using UNIX I/O redirection operations, **file.unix** is redirected as input to **tr**(**1**), and **file.dos** is created and will contain the output.
-lab46:~$ cat output | sed 's/^\(REGEX1\- \(REGEX2\) - \(REGEX3\) - \(REGEX4\)$/\2:\3-\4:\1/g' | less +
-81559:ACCT 1000-001:Accounting Practices +
-82350:ACCT 1000-003:Accounting Practices +
-82355:ACCT 1030-001:Financial Accounting +
-81558:ACCT 1030-002:Financial Accounting +
-81107:ACCT 1030-003:Financial Accounting +
-81108:ACCT 1030-004:Financial Accounting +
-81173:ACCT 1030-005:Financial Accounting +
-82115:ACCT 1030-006:Financial Accounting +
-82078:ACCT 1040-003:Managerial Accounting +
-81123:ACCT 1050-001:Accounting Procedures +
-81783:ACCT 1100-001:Federal Income Tax +
-82358:ACCT 1100-002:Federal Income Tax +
-81124:ACCT 2030-001:Intermediate Accounting I +
-82359:ACCT 2030-002:Intermediate Accounting I +
-82361:ACCT 2100-001:Computerized Accounting +
-81139:ANTH 2120-001:Cultural Anthropology +
-82296:ARAB 1010-001:Elem Mod Stand Arabic Con&amp;StrI +
-82297:ARAB 1010-071:Elem Mod Arabic Con&amp;StrI - lab +
-81505:ARTS 1004-002:Introduction Art Appreciation +
-81771:ARTS 1030-001:Drawing I +
-82176:ARTS 1030-002:Drawing I +
-82112:ARTS 1030-003:Drawing I +
-81503:ARTS 1030-004:Drawing I +
-81151:ARTS 1210-001:Ceramics I +
-82110:ARTS 1210-002:Ceramics I +
-81504:ARTS 1210-003:Ceramics I +
-82134:ARTS 1210-004:Ceramics I +
-81176:ARTS 1210-005:Ceramics I +
-81873:ARTS 1220-001:Basic Black &amp; White Photo +
-81874:ARTS 1220-002:Basic Black &amp; White Photo +
-81875:ARTS 1220-003:Basic Black &amp; White Photo +
-81180:ARTS 1310-001:History/Appreciation of Art I +
-</cli>+
  
-<WRAP round warning box>**__NOTE:__** If the format of your data does not seem to change, you've got a typoor a RegEx that doesn't adequately describe the data. Go over your syntaxlook for any possible gotchas. Ask questions, seek clarification, **and don't be afraid to have someone look at your pattern**... you'd be amazed what a second pair of eyes can do.</WRAP>+In the **filters/** subdirectory of the UNIX Public Directory you will find some text files in DOSMac, and UNIX format.
  
-Once you get it--- **WOW!** the data changed, just the way we wantedNow we can do further analysis and write shell scripts that better assist us in our tasks. **Activities like this is what separates someone who can effectively command technology as a tool to assist you** to someone who resorts to manual data entry, racking up hours of time manually preparing the data to attempt to answer the same questions we've asked and gotten answers toAnd our processing takes a fraction of the time it would take compared to trying to do all this data filtering and rearranging by hand.+^  4.  ^|Let's do some **tr**(**1**) conversions:
 +| ^  a.|Convert **file.mac** to UNIX format. Show me how you did thisas well as any interesting messages you find inside.| 
 +|:::^  b.|Convert **readme.unix** to DOS format. Same deal as above.| 
 +|:::^  c.|Convert **dos.txt** to Mac format. Show me the command-line used.
 +=====Procedure===== 
 +Looking back on our database (**sample.db** in the **filters/** subdirectory of the UNIX Public Directory), let's do some more operations on it:
  
-That is the power of Regular ExpressionsWe can effectively delegate the manual labor to the computerwhich is very good at manual (and menial) tasks, and is great at following instructions.+^  5 ^|Developexplain, and show me the command-lines for the following:| 
 +| ^  a.|How many unique //students// are there in the database?
 +|:::^  b.|How many unique //majors// are there in the database?
 +|:::^  c.|How many unique "favorite candies" in the database? (remove any trailing asterisks from the output)|
  
-Plusthe less we are involved at the grunt-work level, the less chance there are of errors being introducedThe computer, when it follows correct instructions, will process the data effectively, versus the unpredictability of human manually working on the dataaccidentally inserting typos or other glitches that would threaten the validity of the end data+<WRAP round info box>**__HINT__**: sort them in alphabetical orderand make sure there are no duplicatesAlso- make sure you don't count the title banner as "student". Also be sure to either omit the header, or have the header at the top of any provided output
-=====Additional Data Wrangling===== +</WRAP>
- +
-To cap off our experience, let's do one last foray into rearranging our data.+
  
-^  9.  ^|Rearrange the course information as follows (and show your command-lines):| +^  6.  ^|Using the **pelopwar.txt** file from the **grep/** subdirectory of the UNIX Public Directory, construct filters to do the following:| 
-| ^  a.|PREFIX NUMBER-SECTION(CRN):TITLE+| ^  a.|Show me the first 22 lines of this file. How did you do this?
-|:::^  b.|PREFIX NUMBER:CRN (omit the section and title)+|:::^  b.|Show me the last 4 lines of this file. How did you do this?
-|:::^  c.|PREFIXNUMBER-SECTION:TITLE (CRN)  (merge PREFIX and NUMBER togetherno space separating them).|+|:::^  c.|Show me lines 32-48 of this file. How did you do this? (HINT: the last 16 lines of the first 48)
 +|:::^  d.|Of the last 12 lines in this fileshow me the first 4How did you do this?|
  
-<WRAP round info box>**PLEASE- ASK QUESTIONS, SEEK CLARIFICATION**. You're all just starting out, developing a proficiency with Regular Expressions. Typos happen. Don't let them trainwreck your progress on the lab.</WRAP>+Being familiar with the commands and utilities available to you on the system greatly increases your ability to construct effective filters, and ultimately solve problems in a more efficient and creative manner.
  
 =====Conclusions===== =====Conclusions=====
haas/spring2014/unix/labs/laba.1383759939.txt.gz · Last modified: 2014/03/22 19:44 (external edit)