Monday, February 8, 2016

Text Processing and Unix History

Preparing for the RHCSA certification is turning out to be a heap of fun! Despite more than 20 years as a free software activist and personal user of GNU/Linux systems for all my personal computing, and despite being a computer science teacher during that same time, there are a wide range of basic Unix CLI skills that I only scratched the surface of in all that time (shame on me!).

Preparing for the RHCSA is providing the opportunity to address that deficit at long last.  Chapter 4 of the book we are using in class to study for the certification is titled "Working with Text Files". The most enjoyable thing about this investigation into Unix text file processing is the view it provides into Unix history.

In the beginning there was eded begat ex, and ex begat vi... Along the way we got cousins grep and sed too.  Since grep, sed, and vi are part of the Unix admin's toolset, I want to learn to use them at least well enough to be able to help prepare students (and myself) for the RHCSA certification and to be able to present them well to future students in my ITN 170: Linux System Administration class.

Since in the beginning there was ed, let me start with that.  I found a very nice blog post, Actually using ed, which I found to be a wonderful introduction to this tool.  I set myself the task of using ed to create a list of fruits in a file named fruits.txt.  The first thing I found out was that trying:
$ ed fruits.txt
did not create the file for me, instead returning a "No such file or directory" error.  So I did the following, which worked:
$ touch fruits.txt
$ ed fruits.txt
0
a
apples
bananas
pears
apricots
kiwis
blueberries
oranges
peaches
cranberries
blackberries
pinapples
teaberries
.
w
110
q
$
After that, I ran $ cat fruits.txt, and saw that everything was as I wanted it:
apples
bananas
pears
apricots
kiwis
blueberries
oranges
peaches
cranberries
blackberries
pinapples
teaberries
Now if I want an alphabetical listing of the fruits in my list, I can run:
$ grep berries fruits.txt | sort
and see this:
blackberries
blueberries
cranberries
teaberries
RegexOne is a nice, interactive tutorial for learning basic regular expressions.  I wanted to do all the exercises using grep on the command-line as well, and in the process setup a new github repo for resources related to our RHCSA study, here.

Next I wanted to learn sed.  Sed - An Introduction and Tutorial by Bruce Barnett is a wonderful tutorial.  With so much awful document out there, it is great to find something written by someone with a grasp of how people actually learn.

Using the fruits.txt file I created with ed, I ran $ sed s/berries/cherries/ fruits.txt and got:
apples
bananas
pears
apricots
kiwis
bluecherries
oranges
peaches
crancherries
blackcherries
pinapples
teacherries
Since sed uses the same substitution syntax that vim uses, learning it will be a big help in becoming a more effective vim user as well.

No comments:

Post a Comment