Finally we have gotten to the point where we are developing the necessary skills to do some data manipulation! In this lesson we'll learn just a few simple unix-based commands that can make your life easier and faster. We'll learn to read in a data file, and do some computation with its contents.
By the end of this lesson, you should be able to:
This lesson will take us one week to complete, 30 June - 6 July 2021. The deliverable for this lesson is only a reading discussion. The Unix exercises are all in a non-graded form ("Try This!", "Quiz Yourself", etc.) but you should take the time to work through them anyway because knowing some of these simple tricks will save you a lot of headaches in Lesson 8 and anytime you want to import a dataset into Processing to make a plot out of it.
If you have any questions, please post them to our Questions? discussion forum (not e-mail) in Canvas. I will check that discussion forum daily to respond. While you are there, feel free to post your own responses if you, too, are able to help out a classmate.
This week we will read and discuss two papers:
Tversky, B., J.B. Morrison, M. Betrancourt, 2002, Animation: can it facilitate, Journal of Human-Computer Studies, 57, p. 247-262.
Höffler, Tim N., and Detlev Leutner, 2007, Instructional animation versus static pictures: A meta-analysis, Learning and Instruction, 17, p. 722-738.
These papers try to compare learning outcomes from still visuals with those from animations to see if animations are really better, or not.
As you read, consider the following questions, which we will discuss as a class:
Once you have finished the readings, engage in a class discussion that will take place during Lesson 7. This discussion will require you to participate multiple times over that period.
You will be graded on the quality of your participation. See the grading rubric [1] for specifics.
This short tutorial is meant to provide you with familiarity using a small number of unix commands to manipulate big text files of data. It is not meant to substitute for a complete understanding of unix, or programming in general, or even an exhaustive listing of useful commands but I hope that if you follow along, you'll learn enough simple file editing skills to save you some time.
The first thing we'll want to do is open a terminal window. Go to the Utilities folder inside your Applications folder and open Terminal. A window appears with some text that should be similar to the following.
Last login: Tue Apr 20 09:43:13 on console [rockfall:~] eliza% |
The first line "Last login: Tue Apr 20 09:43:13 on console" provides the date, and time from the last time that you logged into the system. On the second line, the word rockfall refers to the machine that you are logged into. In this case, rockfall refers to my machine or hard drive. The text after the : refers to which directory I am in. The ~ means that I am currently in my home directory. So, [rockfall:~] means that I am logged into the home directory on my machine. Next, "eliza" refers to my userid and the % means that is the end of the prompt and is waiting for your input.
An image of the window with the text referenced above is below.
Here are three commands to try:
Now let's take a text file and mess around with it using unix commands in the terminal window. Here is a link to a plain text file of ten days of aftershocks [2] following the 4 April 2010 Baja California earthquake. Put it in the new directory you called earth801/data1. Go to earth801/data1 and type ls to verify the file is there. Type more baja_neic.txt (in which baja_neic.txt is the actual name of the text file). The file should look like the screenshot below. If your terminal window is too small to show the whole file at once, you will get a black bar at the bottom that tells you what percentage of the file you are seeing. Hit the spacebar and you'll see another chunk of the file. Continue to hit the spacebar until you've seen the whole file and you are back at the terminal prompt. Alternatively, if you type cat baja_neic.txt the entire file will scroll by and leave you at the prompt when it's done.
The command head baja_neic.txt shows you exactly the first ten lines of the file. Try it. You can also modify the head command like this:
head -5 baja_neic.txt |
The -5 tells head to show the first 5 lines. Showing ten lines is the default when head has no arguments, so the following two commands are equivalent:
head baja_neic.txt |
head -10 baja_neic.txt |
The command tail is similar to head but works on the end of the file instead of the beginning. The commands more, cat, head, and tail return their output to the screen by default but you can also have them create a new file and put their results in it instead. The way to do this is to redirect the output with the > symbol.
For example, do this:
head -5 baja_neic.txt > newfile |
and you will create a new text file called "newfile" which contains exactly the first five lines of the original file baja_neic.txt. It is important to note here that performing this command has not changed the original file in any way. You can type ls to verify that you now have two files in your data1 directory. One of them is the original baja_neic.txt and the other one is called newfile and it is a copy of the first five lines of baja_neic.txt. Use the more command to look at your newfile file. Did you get what you were expecting? When the "head" command counts lines of a file, blank lines are counted just like lines that have text characters in them, so that's why newfile looks the way it does. At this point, if you have been following along, the following three commands should give you identical output:
head -5 baja_neic.txt |
more newfile |
cat newfile |
Okay, on to the next command of interest. The cp command copies one file to another but instead of using > you just specify the other filename. So, these two commands are equivalent ways of copying the entire file baja_neic.txt to a new file called baja_neic_copy.txt:
cp baja_neic.txt baja_neic_copy.txt |
cat baja_neic.txt > baja_neic_copy.txt |
If you want to rename a file without changing its contents, use mv. Like cp, mv requires two filenames, the previous one and the new one.
mv newfile baja_neic_five.txt |
The above command renames the file "newfile" to "baja_neic_five.txt". You can also use "mv" to change the location of a file. Try typing
mv baja_neic_copy.txt .. /data2/baja .txt |
This command takes the file "baja_neic_copy.txt" and moves it from the folder data1 to the folder data2 and renames it baja.txt. You can go to data2 (remember how?) and verify there is now a file in there called baja.txt and that it is a duplicate of baja_neic.txt.
Another cool use of the cat command is to stick two or more files together and make one file. So,
cat baja_neic.txt newfile > baja2.txt |
will make a file called baja2.txt which is a copy of baja_neic.txt plus a copy of "newfile" stuck together.
This short tutorial is meant to provide you with familiarity using a small number of unix commands to manipulate big text files of data. It is not meant to substitute for a complete understanding of unix, or linux, or even an exhaustive listing of useful commands but I hope that if you follow along, you'll learn enough simple file manipulation skills to save you some time.
If you are on a PC running Windows, you can emulate a unix/linux command window environment by running "cygwin." To reiterate: you aren't running linux, but it looks like you are.
To download it, go to the home of the cygwin project. [3]
I also need to mention the following caveat: I'm not a PC user! When I was in grad school I had a Sun workstation that I used for everything, including typesetting. I didn't even write in Word, I used LaTeX. Now I use a mac. I also had to relax my principles on avoiding Word or face a lifetime of really cranky collaborators, but that's another story. The upshot here is that I am probably less of a complete doofus than your grandparents when it comes to PCs but . . . okay you get the picture. So when I made the screen casts of me attempting to present a tutorial of cygwin, I borrowed a pc and figured it out on the fly. And it basically worked okay; I give cygwin my thumbs up.
The first thing we'll want to do is open a terminal window. Double-click the cygwin icon to open a terminal window. When the window is active there will be a blinking cursor where you can start typing. Unix commands are all typed at the prompt and by default the output of any command you type goes to the screen in the terminal window where you are typing.
Here are some commands to try:
In Part 2, we'll see how to use unix commands to change our location in the computer's file structure. It's analogous to clicking through the various discs and folders from the windows launched when you double-click "my computer" except that it involves no mouse clicks, only typing.
The command of interest here is called cd. The way it works is that you type "cd pathname" at the prompt, and then you will go there (you have to type the actual path, not the word "pathname"). Nested folders have to be separated by forward slashes "/". You can verify that you are where you think you are by typing pwd or by navigating to the same address via windows and noting that the folder contents are the same.
Note that in order to move between the C and D drives, you'll have to start the address with "cygdrive". For example, the command cd /cygdrive/d will take you to the uppermost level of drive D and cd /cygdrive/c takes you to the uppermost level of drive C.
In the rest of this tutorial we'll do some simple things to files using unix commands.
First of all let's take a text file and mess around with it using unix commands in the terminal window. Here is a link to a plain text file of ten days of aftershocks [2] following the 4 April 2010 Baja California earthquake.
The command head baja_neic.txt shows you exactly the first ten lines of the file. Try it. You can also modify the head command like this:
head -5 baja_neic.txt |
The -5 tells head to show the first 5 lines. Showing ten lines is the default when head has no arguments, so the following two commands are equivalent:
head baja_neic.txt head -10 baja_neic.txt |
The command tail is similar to head but works on the end of the file instead of the beginning. The commands less, cat, head, and tail return their output to the screen by default but you can also have them create a new file and put their results in it instead. The way to do this is to redirect the output with the > symbol.
For example, do this:
head -5 baja_neic.txt > newfile.txt |
and you will create a new text file called "newfile.txt" which contains exactly the first five lines of the original file baja_neic.txt. It is important to note here that performing this command has not changed the original file in any way. You can type ls to verify that you now have two files in your data1 directory. One of them is the original baja_neic.txt and the other one is called newfile.txt and it is a copy of the first five lines of baja_neic.txt. Use the less command to look at your newfile.txt file. Did you get what you were expecting? When the head command counts lines of a file, blank lines are counted just like lines that have text characters in them, so that's why newfile.txt looks the way it does. At this point, if you have been following along, the following three commands should give you identical output:
head -5 baja_neic.txt less newfile.txt cat newfile.txt |
Okay, on to the next command of interest. The cp command copies one file to another but instead of using > you just specify the other filename. So, these two commands are equivalent ways of copying the entire file baja_neic.txt to a new file called baja_neic_copy.txt:
cp baja_neic.txt baja_neic_copy.txt cat baja_neic.txt > baja_neic_copy.txt |
If you want to rename a file without changing its contents, use mv. Like cp, mv requires two filenames, the previous one and the new one.
mv newfile.txt baja_neic_five.txt |
The above command renames the file "newfile.txt" to "baja_neic_five.txt". You can also use mv to change the location of a file. Try typing
mv baja_neic_copy.txt .. /data2/baja .txt |
This command takes the file "baja_neic_copy.txt" and moves it from the folder data1 to the folder data2 and renames it baja.txt. You can go to data2 (remember how?) and verify there is now a file in there called baja.txt and that it is a duplicate of baja_neic.txt.
Another cool use of the cat command is to stick two or more files together and make one file. So,
cat baja_neic.txt newfile.txt > baja2.txt |
will make a file called baja2.txt which is a copy of baja_neic.txt with a copy of "newfile.txt" appended to the bottom.
To delete a file or a folder type rm filename. Careful here because the file won't go into a trash folder that you can change your mind about. It is really gone.
All the examples detailed here are accomplished from your terminal window if you are on a Mac, or from your cygwin window if you are on a PC.
The awk command is a powerful way to manipulate the contents of textfiles. We are only going to skim the surface of what awk can do right now. Let's start with a simple example. Say you have a text file that has some columns of numbers in it. With one awk command you can rearrange the columns in a different order, or perform some arithmetic on the columns.
Download the file1.txt [4] textfile in order to follow along with what I am doing. This is accomplished by clicking the link to the filename and then choosing Save As . . . from your browser's file menu. Save it somewhere on your computer, then in the terminal, navigate to that place. Remember how? You'll want to use cd.
This is what "file1.txt" contains. It is simply a plain text three-column six-row arrangement of numbers. The first column is the number 1, the second column is the number 2, the third column is the number 3.
Awk is great for quickly manipulating files that are arranged in columns, so it is a nice way to fiddle around with plain-text data files since those are frequently in columns or tables. It uses some peculiar syntax. Let's say we wanted to display just the first column from "file1.txt." Here's how to do it:
awk '{print $1}' file1.txt
The first thing you type is awk and then put single quotes and curly braces. Inside the curly braces we wrote print $1 which is the command to print column #1. The filename from which we are extracting column #1 goes next.
Let's say we wanted to display just the second column from "file1.txt." In that case we'd type:
awk '{print $2}' file1.txt
You can output any number of columns and put them in whatever order you want. Let's say we want column 3, then column 1 but not column 2.
awk '{print $3, $1}' file1.txt
The comma between $3 and $1 tells awk to put a space between the columns.
Let's say you want to output column 1, substitute 4's in column 2, then output column 3 unchanged. That would be like this:
awk '{print $1, 4, $3}' file1.txt
The 4 inside the the curly braces doesn't have a $ in front of it because it is the actual number 4, it is not referring to a 4th column.
You can do math inside the print statement of awk and you can also deal with columns that aren't numbers. Let's say I want to output the sum of columns 1 and 2 as the first column, my name as the second column, and the product of columns 2 and 3 as the third column, and then make a fourth column that is the number 25:
awk '{print $1+$2, "eliza", $2*$3, 25}' file1.txt
Here's what the output of awk '{print $1+$2, "eliza", $2*$3, 25}' file1.txt looks like.
There are a few special characters. A useful one sometimes is "\t" which tells awk that you want tab spaces in between the columns.
awk '{print $1 "\t" $2 "\t" $3 "\t"}' file1.txt
The command above will output file1.txt unchanged except for tab spaces in between the columns instead of just one space.
All the examples so far have output the results of the awk command to the screen. They have not altered the original file, and they haven't saved the results anywhere. To put the output of awk into a new file instead of showing it on the screen, use >. Let's say I want to make a new file that is the same as file1.txt but with the columns in reverse order:
awk '{print $3, $2, $1}' file1.txt > file2.txt
Now if I look in the folder where file1.txt is, there are two files. file1.txt is still there, but there is a new file called file2.txt as well.
On the left is the original file1.txt.
The command
awk '{print $3, $2, $1}' file1.txt > file2.txt
creates the new file2.txt, seen at right.
vi (also called vim; the two are basically the same) is a text editor that allows you to create and edit text inside a terminal window without popping up another window and without using the mouse. Truthfully, most people would never want to get rid of their mouse if they are used to using it all the time, but if you want to get into a file, do a simple thing to it, such as deleting the first 30 lines or adding one line to the bottom or something like that, then vi is handy. However, the functionality of vi does not lend itself well to making a screen capture "how-to" movie because most of the action takes place on the keyboard and you can't see my hands with a screen capture.
When you are using vi to edit a file, you will either be in "insert" mode or in "moving around" mode. While you are in "insert" mode, whatever you type becomes part of the file. (just like whatever word processor/text editor you are used to). But when you are in "moving around" mode, you use keyboard keys to move the cursor around the file. To get started, type vi filename at the terminal prompt (in which "filename" is your actual filename, not the word "filename" unless that's the name of your file .
Here's an incomplete command list (the man page for vi will do better) but it's a start:
i to insert before the cursor, I to insert at the beginning of the current line
a to insert after the cursor, A to insert at the end of the current line
o to make a new blank line below the cursor and put the cursor at the beginning of it, O to make a new blank line above the cursor and put the cursor at the beginning of it
ESC to get out of insert mode and go into moving around mode.
h moves one space to the left
l moves one space to the right
j moves one line down
k moves one line up
dd deletes the current line
x deletes the current character
typing a number before a command repeats the command that many times, so 10 dd deletes 10 lines beginning with the current line.
:w saves your work
:q quits vi
you can do these together, so :wq saves your work and quits vi all in one step.
Let's put our skills to work. Download this file of a catalog of earthquakes [5] from the USGS. Use vi and awk to make a new file that contains just one column -- the earthquake magnitudes. This is the kind of thing that will be super useful for making a frequency magnitude diagram! Try it on your own and if you get stuck, check to see how I did it. Keep in mind that there is just about always more than one way to accomplish an editing task like this. The point is to get the end result without lots of work and cumbersome steps in a non-mathematical spreadsheet program that was not intended to handle a big dataset.
Links
[1] https://www.e-education.psu.edu/earth801/node/537
[2] https://www.e-education.psu.edu/earth801/sites/www.e-education.psu.edu.earth801/files/images/lesson7/baja_neic.txt
[3] http://www.cygwin.com
[4] https://www.e-education.psu.edu/earth801/sites/www.e-education.psu.edu.earth801/files/text/lesson7/file1.txt
[5] https://www.e-education.psu.edu/earth801/sites/www.e-education.psu.edu.earth801/files/text/lesson7/baja_neic.txt