Lab01 Exercises
Contents
Lab01 Exercises#
UW Geospatial Data Analysis
CEE467/CEWA567
David Shean
Introduction#
Let’s play around with some text files to explore the power of the command line and bash shell.
For the following instructions/questions, please document the command you ran, and the resulting output on the terminal. You can copy and paste everything in a text file, or use whatever method is most efficient. Maybe you want to practice markdown formatting, or if you would prefer to wrap everything into a single shell script, and submit that, go for it.
You will almost certainly need to search for tips on how to answer some of these questions. Please use these external resources, but if you find an explicit solution, don’t just copy and paste the code, review it and try to understand what is actually happening. Note that Stack Overflow and other forums will have excellent information (and probably a lot of advanced information that is over your head) - typically the answer you’re looking for has the most “upvotes” (number to the left of the answer) or some kind of indication as the “accepted answer.”
OK, let’s get started!
Part 1: Look it up in the dictionary#
On your Unix/Linux filesystem, you typically have a dictionary of words (https://en.wikipedia.org/wiki/Words_(Unix)). On OS X, this file is located in /usr/share/dict/words. This file is missing from our barebones JupyterHub Linux distribution, so I added one to the assignment git repo (2.4 MB).
Unzip words#
Fortunately for you, the basic linux operating system we’ve provided includes command-line utilities to unzip files. Navigate to the directory containing the data:
unzip gda_2021_data.zip
This should create a new data subdirectory, with two new files. Check them out with ls -l data
Inspect words#
The
wordsfile has no extension. What type of file is this? (Hint: check out thefilecommand)Inspect the file
more words(hit q to quit). Should see one word per line. (no response needed)How many words are in this file?
How many characters?
How big is the file size in bytes?
How many bytes are required to store each character? Hopefully this provides some new insight into file size on your computer.
Print the first 3 words. Print the last 3 words (do this with a command, don’t just view or open in text editor and copy/paste)
Does
wordsinclude the nickname for the UW mascot (if unfamiliar, ask your neighbor)? If so, what line number?
Extra credit#
How many characters are in the longest word? Hint: should be simple one-liner, you don’t need
awkfor this.How many words end in “ology”?
How many end in “ologist”?
How many have the same prefix?
Hint: replace “ologist” with “ology”, then count number of duplicate lines.
Part 2: Inspect and manipulate tabular data#
The text file GLAH14_tllz_conus_lulcfilt_demfilt.csv contains processed/filtered records from ICESat satellite laser altimetry measurements over the Western U.S. between 2003 and 2009. Don’t worry about details for now, we will explore in the coming weeks. For now, pretend your advisor/boss just sent you this random file (with no metadata) and asked you to inspect and clean up for their old-school analysis tools.
Inspect the file on the command line. Use tab-completion after typing the first few letters of the filename.
The extension suggests that this is comma-separated value (
.csv) text file - verify this.Fortunately, there is a header on the first line containing strings for each field (column) name. Some of these should sound familiar.
How many fields are there?
How many rows contain data (excluding the header)?
How many records are from 2005?
Utilities like
grepandsedoffer powerful functionality with “regular expressions” - one could spend weeks learning these. For now, I’ll give you a hint, if you preceed yourgrepsearch string with a carat^(e.g.,^stringtofind, it will only return records that begin withstringtofind, excluding any records withstringtofindelsewhere in the string.You’ll want to use a pipe
|here to pass the output ofgrepto another command that can count the number of lines.
Create a new file with only the
latandlonfields, separated by a space, and sorted in ascending order bylatvalue.This should be possible without any loops, just need to redirect the stream using pipes
|and>. Let’s break the problem down.First, isolate the
latandlonfields - several ways to do thisPipe that output to a command that will replace the
,with aon all linesPipe that output to a command that will sort the numeric
latvaluesRedirect that output to a new file
Run a quick
headandtailon the output file to verify
Extra Credit: Shell script#
Create and run a script to answer the following question:
How many words begin with each letter of the alphabet (case-insensitive)?
Useful references for review#
Requirements:#
Ignore case, so “A” and “a” are considered the same word
Your script should accept the
wordstext file as an input argument (assign to a variable namedfn)Your script should create a new output file called
words_lettercount.txtPro tip: You can use variable parameter substitution to append strings to the input filename:
out_fn=${fn%.*}_lettercount.txtThe
${fn}construct is a less ambiguous variable reference. Need this if you wanted to do something like print your filename and add extra charactersecho ${fn}:moretext(tryecho $fn:moretextfor comparison)The
%.*can be used to get the first part of the filename string before any.extension (see SO answer)
The output file should include a letter and total count on each line (“a 17096”)
Instructions:#
Create a new shell script in your preferred text editor (nano, vim, emacs or Jupyterlab text editor):
nano myawesomescript.shWrite some code.
Save the script.
Make it executable:
chmod +x myawesomescript.shRun it!
./myawesomescript.sh wordsReview the output
more words_lettercount.txt
Submission#
We will review this process during Friday lab
Use
git addto stage each new file containing your answers/scriptsI recommend you start by adding files one at a time:
git add myawesomescript.shFor now, resist the temptation to
git add .orgit commit -a, as you will inevitably include files that don’t belong in a git repo.
Use
git commit -m "Message"to commit these changes, replacing “Message” with an appropriate commit message.Use
git pushto push your local changes to your remote repo origin on Github