As we can see when it comes to Linux, we deal with a lot of text.
Our SSH session is all text.
We see text output and we input commands as text
and later on we'll find out that configuring applications or
the system itself is also done through text files.
This means we'll have to know how to view text files, edit text files,
transform text files and compare text files.
Let's start out with the most basic thing.
When the file you're trying to view is very small.
You can use the cat command followed by the name of the file that you want to inspect.
Here. We have cat slash home slash users dot text.
Now
to view the file reversed from the bottom up, we use the T command.
But what if the file is very long?
This might happen when you want to view a log file
where a program records all status messages about what it did,
what succeeded, what failed et cetera.
Many times when you deal with logs,
you'll want to see what happened in the last few seconds or minutes.
You'll find those log lines at the end or the tail of this file,
you can view the last lines with a command like this.
Now by default, the tail command will show you the last 10 lines.
But you can tell it how many lines you want to see with the dash N option.
The opposite of this command
to view the first few lines instead of the last ones is the head command.
We have nine lines of text here.
But the first one to log is an empty line, bringing the total to 10.
We have nine lines of text here.
But the first one in the log is an empty line,
bringing the total number to 10 to specify how many
lines to return from the start of the file.
We use the same dash N option.
Now,
sometimes we'll have to deal with large files
that require many changes in many places.
Manually editing and changing the same thing in many lines of text can be tedious,
but we have tools that can help automate these tasks.
Now imagine you have a file with this content.
A list of people, their addresses, phone numbers and citizenships.
We see that Canada is spelled incorrectly in some places showing up as
KDA instead missing an A in the middle.
We would like to replace all occurrences of Canada to Canada.
There's a utility called said that can search and replace all the required text.
The names
comes from stream editor,
a stream of text energy utility.
It gets transformed in some way and then
it outputs that modified stream with this command.
We can preview what said would do without actually changing the file.
And it's recommended that you first preview what would
happen to make sure that there's no mistakes.
Let's break down how our command works.
Now, here we have said S slash can slash Canada slash G userinfo dot tech.
Now, the easiest part we see here at the end,
we pass the name of our file or the path
to that file if it's not in our current directory.
Now, S slash can
slash Canada slash G is obviously where the magic happens.
We must wrap this between single quotes to make sure
that our command interpreter does not interfere with the content.
In here, we can have special characters like an asterisk.
And if we don't wrap it between these quotes, bash,
our command interpreter might transform that into something else
because bash interprets what we want that asterisk to do into
something special by wrapping our content between these single quotes.
Bash knows that it should not interpret anything between those quotes.
Now, the first S here tells,
said that this a substitute command for search and replace.
Then we tell it to replace it with Canada.
And finally, the last G is for global search and replace,
normally said would only replace the first thing
it finds on each line by passing global.
Here we tell it to search and replace all
occurrences even if there are multiple per line.
So in this command, for example, without the global option,
we see that only the first occurrence of Canada has changed to Canada,
the remaining occurrences of Canada on each line are left as they are.
That's what the global option helps with to change all occurrences on each line.
Not just the first one.
Now up to this point said just showed us what it would do but not edit the file.
If we look at the contents of the file, after running the S command,
we see it's still the same.
When we're happy with the preview,
it shows us we can tell it to actually edit
the file by passing in the dash I command-line option
dash I is short for in place to edit in place directly in the file.
We won't see any change on the command-line.
But if we were to run cat on user info,
we could see that the changes were made to the file.
Now, let's say we would like to extract the names alone from this file.
The first column in the file
for this, we would use the cut command.
So cut is a utility that can extract the parts we need from a file.
For example, if we only want the first column that appears on each line,
we would specify cut
with the dash d to specify the delimeter
words are separated by a space. So we add a space wrapped between single quotes.
This tells us that the space character is the one separating the parts we need.
We can use anything as a delimeter.
For example, if we add numbers such as 10.22 or 12.34 and so on,
and we want to extract only the integer part.
We would use a dot as a delimeter and we
would be able to extract 10 and 12 from those numbers
with dash F. We specify the fields we want to extract. In
this case, each word is a field and we want to extract field one,
the first word on each line.
Let's look at another example.
Let's say that this other file separates different entries with commas
and we would like to extract all of the countries from this content.
Now, countries are the third field in this case.
Also, after we extract our country names,
we want to tell our command to save this
output in a new file called countries dot text.
We'll do that with a simple redirection.
Now we'll learn more about redirections in a later lesson for.
Now,
all that you need to understand is that we can redirect with a
greater than sign and that will save the output to a file.
Now, for this purpose, we run the command cut dash D,
then we follow up with the delimeter A comma
and with the slash F three, we extract the third field,
then we pass the name of our file,
userinfo dot text and we redirect the output to countries
dot text and that creates a file with this content.
Now, in the countries dot text file,
we have a list of countries but they are duplicates.
We'd like to get a list of the unique countries from this file so
that we know the total number of different countries the users originate from.
For this purpose.
We can use the unique command and provide the file name to get unique entries from.
But check that out when we look at the output that our command generated,
we can still see some duplicates.
But why all that the unique command does is
remove repeating lines that are next to each other.
In this case, it removed the two lines that mention Canada at the end,
but it didn't remove the duplicates that aren't right next to each other.
So how do we get all similar lines to be adjacent
with the sort utility?
This sorts the entries in the file alphanumerically,
which means countries with the same name will be next to each other.
Next, we can pipe the output of the sort command to the unique utility.
We'll learn more about piping output in a later lesson.
Piping simply passes the output of one command to another command.
This command will finally give us the result we were looking for.
Now imagine that an upgrade changed an old configuration file,
the old configuration file had 250 lines of text.
The new one has 268 lines.
We could open them both and analyze it line by line to see what changed.
But it would be hard to do this.
And that's why we have tools that can analyze
files for us and quickly show the differences.
Imagine the files on the right
lines,
one and four differ but lines two and three
are identical in both files with the diff tool.
We can see the differences between these files with a command like this.
And we'd see output like this.
Since it focuses on differences, the identical lines are not shown here,
but we see some useful output. One.
C one tells us that line one from file one is changed.
That's the C in line one from file two.
Now, otherwise line one in file one is different from line one and file two.
Now, the less than sign tells us that this content exists in the first file,
the file at the left,
the greater than sign tells us that this content exists in the second file,
the file at the right
now in large files, we might have no idea where these lines are situated.
So we can tell diff to give us a little bit of context.
This shows us what other text is around these areas and where the lines are different
to show context. We use the C option.
We'll see lines that differ marked with an exclamation point, A plus or a minus sign.
The identical lines won't be marked in any way.
The exclamation point signals,
the difference between lines while plus or minus
signals content that was added or removed.
And probably the easiest way to spot differences is
by doing a side by side comparison with the
Y option,
an equivalent command that is easier to remember is Sdiff file one file two,
think of the expression side by side diff and you'll remember the Sdiff command.
We'll see the differences marked with this pipe symbol.