In today's lab, you will be writing files. As you will have seen in the lectures, you can write and append to files using output redirects from commands:
echo .#!/bin/sh. > myscript.sh
This is very convenient for command output but it's not a pleasant, interactive editing experience. For that, you will need to use an editor! In this course
we will teach you how to use vim (a modern version of the powerful vi editor). Vim is a powerful plain text editor, meaning that it writes pure ascii text without
markups (so no fonts or formatting like you'd see in a word processor like Microsoft Word or TextEdit). To open it, simply type vim and the name
of the file you wish to edit.
The big difference between vim and other editors is that vim has multiple modes:
Normal mode is what you get when you open vim. In this mode you can navigate the file, but you CANNOT ENTER TEXT! Keypresses will do
unusual things when you press them. For example, G and / allow you to move and search as you would in less.
You can move about the file using arrow keys or h,j,k,l keys.
To return to normal mode from another mode, press the Escape key.
Insert mode allows you to edit text. To enter insert mode, type i.
Command mode allows you to enter commands. To enter command mode, type : and then enter your command. Press Enter to
execute your command. Important commands are w to write (save) a file and q to quit vim.
Vim has additional modes for selecting text and replacing text, but those three will be enough to get you started.
That should be enough to get you started. If you want to run through a full tutorial on vim, run vimtutor. For a
quick cheat sheet on vim commands, see https://vim.rtorr.com/.
HELP! I am stuck in vim! Press the escape key to return to normal mode. Then save and exit by typing :wq.
If you wish to exit without saving, type :q!.
Questions
What is the difference between the following sets of commands?
cd /lab cd ./week3 head foo.txt
cd /lab/week3 head foo.txt
head /lab/week3/foo.txt
head /lab/week3/../week3/../../lab/week3/./././foo.txt
What would you expect to see in each of the following files, based on the file extension?
stuff.txt
stuff.sh
stuff.jpg
stuff
There is a copy of Frankenstein in this week's directory, but it is compressed (zipped).
How big is the compressed copy of Frankenstein?
Compress Alice in Wonderland. How does it compare to Frankenstein?
Uncompress both files and compare their uncompressed sizes.
The following questions refer to arcade.csv, which holds user data for an online game. Solve them using
cut, sort, head, tail, uniq.
How many teams are there?
How many unique score values are there?
List any duplicate passwords
Which player had the top score? Note: You will need the sort flags -k, -t and -n for this. Check the man page!
Write a pipeline that prints which machine was used by the most players. You will need uniq -c
You are adding a high score screen for your game which will display the Score column first, then the Player, then Team. No other
columns will be shown, and data should be sorted with the highest score appearing first. Write a series of commands to do this
using cut and paste.
Your friend has just sent you some data for her own game in cool_online_game.csv. Use comm to compare this
with your data in arcade.csv.
How many players played both games?
Which players only played your friend's game?
In the week3 directory there is a script called runme.sh, but it is not executable. Modify this
script so that any user can run it with ./runme.sh.
In the week3 directory there is a file called DENIED.txt. Print the contents of this file to the terminal.
Your friend has a broken keyboard, and has written an email (woonky.txt) to the keyboard company asking for repairs. Unfortunately, the
keyboard problem has ruined the letter. Write a pipeline using tr to fix the email.
Use head, tail and | to construct a pipeline which prints line 999 from Alice_in_Wonderland.txt.
A pipeline is a series of commands connected together by pipes (eg. date | wc).
There is a fun command called cowsay which works like echo but adds some decoration to the input string. To try it out,
type cowsay moo. We will make our own version of cowsay called sandwichsay that prints the input string
between two slices of bread. For example, ./sandwichsay "ham and cheese" should look like this:
%============================%
ham and cheese
%============================%
Each slice of bread should be approximately 30 characters wide. Feel free to change the appearance of the sandwich if you like.
Note: Make sure you surround your input string in double quotes ("input") so that it is treated as a single command-line
argument by your script. Otherwise each space in your input string will begin a new argument.
Modify sandwichsay so that instead of printing to the terminal, it appends output to sandwiches.txt.
Bonus
Which player had the top score for the BLUE team?
Can you make cowsay print something other than a cow?
The command ps lists running processes. Can you make it print all processes, not just the processes belonging to your user?
Department of Computer Science & Software Engineering
The University of Western Australia
Last modified: 8 February 2022
Modified By: Daniel Smith