Hi, this is Gary with MacMost.com.
Today let's look at working with Text Files using Command Line Functions and Apps in the
Terminal.
MacMost is brought to you thanks to a great group of more than 700 supporters.
Go to macmost.com/patreon.
There you can read more about the Patreon Campaign.
Join us and get exclusive content and course discounts.
So let's say you're in the Terminal like this and you want to work with some basic text
files.
There's a lot of different commands that you should know.
First, let's create a new text file.
To do so we're going to echo some text.
We're going to use the Echo command and then we'll put some text in single quotes.
This will simply just repeat the text.
But if we use the same command, I'm going to use the up arrow to repeat the command,
and I'm going to use greater than> to send the result of this Echo function to a file.
So let's call this test.txt.
Now I'm actually at the desktop so you should see the file appear over here.
So I'll hit Return and sure enough there's the text file.
Let's double click it to open it up in TextEdit and see what we've got.
So there you can see it just contains that text.
Now what if we wanted to view that in the Terminal.
We can use the cat command.
So I'll use cat space and I'll type the name.
Now I don't have to type it all out because there's only one file on the desktop.
If I type t and then hit tab it'll fill the rest in since there are no other files that
begin with the letter t.
I'll hit Return and it will show me the contents of that file.
Now cat is short for concatenate.
So what it actually can do is show us the results of several files, one after the other.
So let's rename this test file using the Move command, mv.
I'm going to make test.txt to test1.txt.
Now you can see it changed names right here on the desktop.
Let's create a new text file.
I'm going to up arrow to go back to this and I'm going to change the text here to this
and I'm going to have it save this out to test2.txt.
So now you can see I've got two files, test1 and test2.
If I do cat test1.txt I would get the contents of that file.
If I also add to the line test2.txt it'll show me both files concatenated.
I can send this to a new file.
We'll call this test.txt and it creates this new file here.
If I open it up in TextEdit I can see it has both there.
So this is how I can merge files.
I've merged two files here but I could merge many more.
Now a lot of times when you want to work with text files you already have a text file.
So in this case I've created a new test file that is just a list of 500 random names.
So let's work with this one in the Terminal.
Let's say I didn't want to see all of the lines of this file.
I just want to see the first ten lines.
I could use the command head and type the file name.
It gives me the first ten lines.
tail and the file name will give me the last ten lines.
I could change the number of lines by using something like tail or head dash n and a number
like 5 and the name of the file.
It just gives me those five lines.
Now what if we want the entire thing.
I can type cat and the name but it's going to scroll up 500 lines.
Instead I can use less.
Less is a little app that's a viewer for you to be able to see text files.
So, less test.txt and now I see all the lines but it stops there at the bottom.
I can use the arrow keys to go down and up and even spacebar to jump by page.
So I can navigate through this file to see what's in it.
I'll use Q to quit.
Now with a lot of terminal apps like this there's a ton of functionality.
So you want to use the manual, man, and then the name of the command and then you get this
full manual here that tells you everything you could do with the file.
I could hit Return to go line by line or spacebar to jump through pages and read more about
Less or any of these other commands I'm using to see what they are capable of.
Let's really work with this file.
We can use the sort command to sort a file so every line is sorted alphabetically.
So, sort and then test.txt.
It's going to just give the output right to the screen.
So we get 500 lines scrolling up and you could see they are in alphabetical order.
Now if we wanted to output this to a new file we could do sort, then the name of the file,
then dash o, and the name of the new file.
So, sorted.txt.
Now we could see we get this file here which is a sorted list.
You could look at the manual page for sort to find all sorts of cool things you could
do with it including to be able to randomly shuffle the line.
So sort and then dash and a capital R, and then test.txt.
Now we get those 500 lines but in a random order.
We can do it again and it's a new random order.
There's also a command called wc which will give you a word count.
It actually gives you more information than that.
So here it tells us that there are 500 lines, 1000 words and 7056 characters.
Now what if we wanted to search through this.
I can use grep to do a search using Regular Expression.
So let's look for the name Gary in this random list of names.
You can see it comes up with one line that has the name Gary in it.
Let's do the same thing but maybe for Ben.
We'll see we actually have three lines, a Bentley, a Benjamin, and a Bennett.
In one case it's a last name.
We can use sed if we want to actually change the file.
So we include a command in this.
s for substitute.
Let's say we want to substitute every capital T with an exclamation point.
! like that.
Then we do test.txt.
It will give us the results here and you could see the T's were replaced with !. You could
do it for a group of characters.
There's a whole bunch of other commands if you look at the manual for sed.
Now you can send this to a new file but if you actually use sed with dash i then it will
actually do it in place.
So it would change the file right there.
So that's a little dangerous but it may be what you want to do in a particular case.
A more powerful command for doing even more with text files is awk.
With things like grep, sed, and awk manual pages are huge.
There are tons of websites that go into detail about all the different things you can do
with them.
But let's look at an example here.
If I use awk with dash F and then quotes around a space it's going to look for spaces and
it's going to breakup the file by spaces.
So maybe another type of file you might want to break it up by tabs, or by commas, or semicolons.
Then it's going to output just the first element.
So we know every name is a first name and last name.
In this case we're just going to get the first name.
So here if I hit Return you can see it outputs all the first names.
If I do the same thing but say give me the second element on each line it outputs all
the last names.
Here's a command that will do the same thing, break each line apart by spaces but it's going
to output the last name and then a comma and a space and then the first name.
What if you want to work with these files directly.
Of course you can use TextEdit or a third party text editing app but there are two text
editing apps inside of Terminal.
One of them is vi.
You can use this to go into editing mode here.
I can use the arrow keys to move around and I can edit text.
Now vi's are really complex text editor that dates back decades.
The best reason to use vi is if you've already used vi before and you kind of have become
an expert with it.
So a lot of people that were using computers in the 60's, 70's, and even 80's are pretty
good at vi.
But otherwise you want to skip vi and go with a much easier little text editor, nano.
Nano actually has little commands at the bottom so it helps you figure out what you're doing
and it works a little bit more like a regular word processor, like TextEdit, would work.
Then you can make changes to this file, save them, and exit using little control commands
and work on text files without ever having to leave Terminal.
So there's a starting point for using the Terminal to be able to work with text files.
Certainly commands like sed, awk, and greb are good reasons to work with text files in
the Terminal.
There's no reason you can't combine these techniques with desktop apps.
So in other words work with the file in TextEdit to edit it but maybe every once in a while
use a greb or an awk command to perform special functions.