[Autogenerated] Now look at this demonstration time and we're going to
be able to show you that even without using grip,
we still can process our text files.
So we're gonna be taking a look at that internal field
separator and working with that,
we're gonna be able to see how we can process different data formats.
We're going to be looking at space separated.
So that's our default I F S comma separated and we're going to
be able to see the use of the T R command.
So we can translate a space separated file to be a comma separated
file as well as then looking at the coon separated files which make
our file such as our E T C pass W D file,
the shadow file, the group file and so forth.
So we need to go out and find our command line.
I do emphasize that um we, you know,
I always do this because I want you to learn.
And the best way that you're going to learn is by
getting some hands on the practice.
So who's in for joining me at the command line?
And yes, just look at this, the glorious command line,
the glorious command line where we can learn about processing text file.
So I've just created ourselves a test directory
where we can create our files in.
I've moved into that directory and we can now
create our employees dot Text file.
This is going to make use of just the default space separating.
So I've got my user Joe spaced at her age of 32 she
works in the sales department.
We can then go through and app end through to the employees dot Text file.
This doesn't exist.
It's a brand new directory.
So we'll be creating the employees dot text.
Now that really wasn't 2D difficult, was it?
So let's up our ee and we can create a second entry.
So let's create an entry for Rohingya.
Maybe Rohingya is the same age,
but really the values we put in doesn't really matter.
And we'll say that Rohingya works in our I T department so we
can append that to our employees dot tech.
So we've got two records.
Now,
we can always go through and take a look at the employees dot
text file just to see what we've created.
But we've got the two rows each row having the three fields.
Now, our default field separator is a space.
So that's going to work well, for us,
we can create a simple loop either at the command line or in a script,
but we can easily do it from the command line.
So whilst read into the name age and department variables,
we can end that line with a semicolon and the do then will open up our
secondary prompt where we can put the iteration.
Now, for here we want to, then for each row that we find,
we can echo out the name variable, we can echo out the age variable.
And of course, we can go through an echo out the department variable.
And of course,
these are the fields that I've added in and the variables that I've added in.
But if you want to add in different fields and different variable names,
then it really gives you the idea of linking everything
that you're seeing on the screen together.
We can then end our iteration with a done keyw word.
But here on the same line as done,
we have to read in from our employees dot text file.
If I hit the big entity,
we can then see that we're printing out the three
separate fields on separate lines.
Its not necessarily formatted in a nice way,
but it gives you an idea of what can be done.
But we'll be seeing later when we look at our past W D file,
how we can indent our data below the user name.
So there's a lot that we can play around with.
Now,
if we want to create a CS V file and very often data
is presented to us in a CS V file,
we can use the TR command so we can translate our spaces.
Make sure that you've got a space inside of your quotes through to a comma.
And again,
make sure there's just the comma inside of our quotes we can
read in from the employee's text file and look,
we're writing out to the employees dot CS V file.
We can take a look at the CS V file and we can see it's exactly
the same data but separated through commas.
So again, looking at some of the very simple utilities that we've got.
How easy it was to edit that file.
If I app array, I can see my wire loop as it was before when we are array,
it all shows on one line,
but I can easily then go through and change the employees
dot text file to employees dot CS V.
But we can see as we're looking for spaces and don't see the spaces.
We're reading everything into the first variable rather
into the three separate variables.
So this is where our internal field separator comes in the I F S.
Are we ready to edit that?
So let's go through and store our current I F S in the old I F S variable.
Now,
we can set our internal field separator to be the
correct value to support our data.
So literally just the comma with that done.
Now, if I our row key back to my looping structure reading in the CS V file,
we get the data being presented correctly and we can
see how easy it is to set that.
But don't forget that we need to set our I F S back to what it was.
So we can always, at the end of a script go through and reset our values.
Our I F S equals dollar old I F S.
Don't forget the dollar once we've got that in,
we can hit the big entity and we are done and dusted with that.
Now, we might be presented though, with data in many different formats.
One of our formats that we have quite commonly inside of
learn is our colon separated files.
So if we clear our screen and go back and restore our
current I F S and then set our I F S again.
But this time we're going to set it to be the colon the two dots.
This is the format of our E T C pass W D file so we
can then write our wire loop.
But we have more than three fields in here.
The password file has seven fields.
So we then need to put in those seven fields.
And for ease, I'm going to keep them relatively small as we saw with the slide.
So the name, the password, the user ID, the group ID D,
the JI co ss or comments field,
D for our home directory and S for our default shell.
So we're reading in the seven field into seven variables.
Now, reality when we're doing this, we probably put longer variable names,
but it would be easier if we were writing this in a
script rather than the command line.
I'm just thinking of real estate that I have on my command line at the moment.
So we have our semicolon, open up our iteration using the do keyword,
hit the big enter key and now we're ready to start printing our data.
Now, we're going to have one echo command now,
but we're going to use echo minus E minus E is going to allow us
to embed characters into our echo statement.
Now,
we're going to use double quotes so we can make sure that we read
those embedded characters and we're going to have a little bit of
formatting in front of our field.
So we've got user colon and then it's going to be dollar N for our user name.
Now, after this though, we're going to embed a new line.
So back slash N after the new line, we're going tab in.
So we're indenting our user information underneath the user name.
So slash N slash T and this is going to be common after all of our variables.
Next variable up is going to be our password.
So we're going to have password colon and then dollar P,
we're going to run a new line and a tab again.
And next up,
we're going to have our user id so we can use then maybe an
uppercase U ID to represent that followed by a colon,
followed by dollar eu making sure that we've got the right
variables ____ __ against their correct name.
A new line and a tab.
And then we're going to have our group ID variable,
which again,
we can type as upper case G ID and then follow the
variable with a new line and a tab.
Next up, we're going to be taking a look at the comment,
which is our variable dollar C.
And of course, we're going to follow that with a new line and a tab.
After this, we're going to have our directory so we can call this maybe home,
followed by our colon and then putting in the dollar D
variable followed by guess what, a new line and a tab.
Finally, we're at the end.
Now,
we can go through and put our default shell and that's
going to be represented by our dollar S variable.
And we don't need to worry about anything after this.
So we can then go through and close off with our double
quotes and then we can close off our loop with done and
reading in from our E T C pass W D file.
Now, if we're brave enough and if it looks good to you,
we can hit the enter key.
Do you want to press the enter key?
Go on, go on.
Shall we do it?
Yes, let's go through and press the enter key.
Now, it may have seen a little monotonous going through and setting that up,
but we can see how quickly we've retrieved the information
and returned it in a very readable format.
We can see then the properties of each of our users being
indented with the tab below the user.
So this is working really well for us and just showing you
some of the simple things that we can do in processing our
text files in different formats.
But why don't we return to the slide?
So we might properly summarize what we've seen.